Reputation: 15
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!
Upvotes: 0
Views: 178
Reputation: 417
As PetSerAl said adding the empty brackets to the string will work.
[string]$ComputerName becomes [string[]]$ComputerName
This allows for the data to be treated as an array, not just a string of characters. Look at the below as an example.
#Using just [String]
[string]$data = 'this','is','my','array'
$data[0]
This will output just the letter t, because it takes the first position which is the letter t.
However, we are working with an array, not just a string of letters. So when we add the empty brackets
[string[]]$data = 'this','is','my','array'
$data[0]
This now outputs the word 'this' because it is being treated as an array, and the first item in that array is "this".
It's also worth mentioning that your second script worked because you declared it as an array using = @()
Without that, it's just a string variable.
Hope this helps! I'm new to powershell as well but boy isn't it fun learning it?
Upvotes: 1