I am trying to customize a script to give me a report on certain items on multiple servers and having issues.
Basically I am running a command against AD to get me the servers I want to run against. The problem is as follows:
For any Get-WMIObject commands I get a: "Get-WMIObject : The RPC server is unavailable.
For any Get-WindowsFeature commands I get a : "Get-WindowsFeature : Cannot convert ‘System.String’ to the type ‘System.String’ required by the parameter ‘ComputerName’.
So, the WMI problem will be expected on newer services as the WMI service is disabled by default. Keep in mind that it’s deprecated, with CIM being the way forward.
For Get-WindowsFeature, it sounds as if your’e feeding -ComputerName an array of strings, which it can’t accept. It can only run against one computer at a time. You’ve defined $ComputerName as a [string] array, not a [string] of one object. Even if the array only has one object, it’s still an array.
Is there a way to get the output from my code below to output it as a string of one object somehow? I am trying all different things but it is not working. I thought the ‘foreach’ command would break it down to a individual item.
#Get computer objects from AD
function GetComputers
{
import-module ActiveDirectory
Get-ADComputer -SearchBase "OU=DSC Managed Nodes,OU=SERVERS,OU=NYC,OU=Americas,DC=lab2,DC=mckinsey,DC=com" -Filter * | select-object name
}
$computers = GetComputers | out-string
Foreach ($ServerNames in $Computers) {
Write-HTML -ComputerName $ServerNames
}
So, $computers is just a collection of strings, which were converted from Computer objects. What is it you want, just computer names? But why as one object? I guess I’m confused about the goal, about how this information is supposed to be consumed.
$computers is already a single string object - Out-String will have arranged that for you. There’s nothing to enumerate. ForEach can’t “break down” a single object; it’s job is to enumerate through a collection of objects.
As I mentioned this script works fine if I remove the portion where I get the computer names from AD and the foreach command and simply hard code it into the ‘Write-HTML’ function as such:
What I am trying to do is to find a way to get all the servers I want to get information on (run the script against) automatically from AD as this script will become a scheduled task and if machines are added that meet the criteria specified in my Get-ADComputer command they will automatically be added to the report and I don’t have to keep modifying the report every time a server is added (assuming I am even told).
Yeah. I think I know why you’re confused. Let’s take it as given that your Write-HTML works if you pass it a single string, or if you pass it a collection of strings (which is how PowerShell interprets comma-separated lists).
This line:
Get-ADComputer -SearchBase "OU=DSC Managed Nodes,OU=SERVERS,OU=NYC,OU=Americas,DC=lab2,DC=mckinsey,DC=com" -Filter * | select-object name
Does not do what you think it does. This does not return a collection of strings which are computer names. It returns a collection of objects, which have a Name property, which contains a string that is a computer name.
This:
$computers = GetComputers | out-string
Is compounding the problem by rendering those objects as one giant string, which PowerShell no longer sees as structured data. ForEach can’t enumerate across the contents of a string, which is what you tried to do.