Rather amateur scripter here, trying to pass lists of computer names to a command so it'll cycle through them and give me just their name, OS and OS version.
$Lab01comps = Get-ADComputer -SearchBase 'OU=Lab01,DC=domain,DC=domain,DC=domain' -Filter '*' | Select -Exp Name | sort
$Lab02comps = Get-ADComputer -SearchBase 'OU=Lab02,DC=domain,DC=domainstate,DC=edu' -Filter '*' | Select -Exp Name | sort
#This first one is more focused on just using a computer name in position 1. Example below code.
function Get-OS {
[CmdletBinding(DefaultParameterSetName="Remote")]
Param(
[Parameter(Position=1,ParameterSetName="Local")]
[Parameter(Position=2,ParameterSetName="Remote")]
[string]$ComputerName,
[System.Management.Automation.PSCredential]$Credential,
[switch]$Raw
)
If (! $ComputerName) {
$ComputerName = $env:COMPUTERNAME
}
foreach ($comp in $ComputerName) {
Get-ADComputer $ComputerName -cred $cred -prop OperatingSystem,OperatingSystemVersion | select name,OperatingSystem,OperatingSystemVersion
}
}
#Example
PS C:\> Get-OS LAB01COMP1
Name OperatingSystem OperatingSystemVersion
---- --------------- ----------------------
LAB01COMP1 Windows 7 Enterprise 6.1 (7601)
#Attempt 2: This one works, but it requires adding to the $osq array before running just the command, with no parameters. Example below code.
$osq = @()
function Get-OS2 {
foreach ($ComputerName in $osq) {
Get-ADComputer $ComputerName -cred $cred -prop OperatingSystem,OperatingSystemVersion | select name,OperatingSystem,OperatingSystemVersion
}
}
#Example
PS C:\> $osq += $Lab01comps
PS C:\> $osq
LAB01COMP1
LAB01COMP2
LAB01COMP3
PS C:\> Get-OS2
Name OperatingSystem OperatingSystemVersion
---- --------------- ----------------------
LAB01COMP1 Windows 7 Enterprise 6.1 (7601)
LAB01COMP2 Windows 7 Enterprise 6.1 (7601)
LAB01COMP3 Windows 7 Enterprise 6.1 (7601)
I know that was probably a pretty big block of code to post here, but I wanted to show all of what I'm trying so far. What I would like to be able to do is something like this:
PS C:\> Get-OS $Lab01comps
Name OperatingSystem OperatingSystemVersion
---- --------------- ----------------------
LAB01COMP1 Windows 7 Enterprise 6.1 (7601)
LAB01COMP2 Windows 7 Enterprise 6.1 (7601)
LAB01COMP3 Windows 7 Enterprise 6.1 (7601)
I feel like there's something simple I'm missing, but my attempts and online help searches have been kinda fruitless. It's a simple thing, to feed the variables to an array before just running the command, but for both the code itself, and the pursuit of knowledge for me, I'd like to know if what I'm trying to do is possible.
Thank you!
[string]$ComputerName->[string[]]$ComputerName