Author Posts

March 7, 2018 at 3:10 pm

I have the following script that errors out;

# Script to check WinRM service state on Servers in the csv
$myarray = $null
$myarray = @()

import-csv c:\DEVSVRS.csv | foreach {
$Server = $_.ServerName
$ServerInfo = get-service WInRM -ComputerName $Server | select -Property Name,status,starttype
$Startmode = Get-WmiObject -Class Win32_service -Property StartMode -Filter "Name='WinRM'" -ComputerName $Server

$OperatingSystem = $_.OperatingSystem

$myobject = New-Object System.object
$myobject | Add-Member -type NoteProperty -Name ServerName -Value $Server -Force
$myobject | Add-Member -type NoteProperty -Name ServiceName -Value $ServerInfo.ServerName -Force
$myobject | Add-Member -type NoteProperty -Name Status -Value $Serverinfo.Status -Force
$myobject | Add-Member -type NoteProperty -Name startMode -Value $Startmode.Startmode -Force
$myobject | Add-Member -type NoteProperty -Name OperatingSystem -Value $OperatingSystem -Force
$myarray += $myobject
$myobject = $Null
$Startmode = $Null

}

$myarray | Export-Csv C:\WinrmDevSvrStatus.csv -NoTypeInformation

Here is the Error:
Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Scripts\CheckWINrmService.ps1:9 char:106
+ ... vice -Property StartMode -Filter "Name='WinRM'" -ComputerName $Server
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand

March 7, 2018 at 3:51 pm

Share the CSV, Let me check.

March 7, 2018 at 4:49 pm

ServerName DNS Name Powerstate OperatingSystem

March 7, 2018 at 8:20 pm

chrodriguez1 –

Dude, two things here:

One, never ever post real corporate data on a public forum. Only use fake name.
Pull this data a replace it with fake names. No one needs to know the real internal server / host names in your environment to help here.

Secondly, the post has you have it is not a CSV, since there are no commas, it is fixed length and the whole OS part is not really a needed thing.

Now a CSV can have any single char delimiter you want, so that space is fine, except for the OS name section, On using Import-Csv you should use the -Delimiter switch to specific that space as your delimiter.
You can do that here, but that OS section will get truncated, which is not really needed for what you are after. You only really need the short name. That first column.

Why are you not just using the Test-WsMan and Get-Service cmdlet for this test?

Description
The Test-WSMan cmdlet submits an identification request that determines whether the WinRM service is running on a local or remote computer. If the tested computer is running the service, the cmdlet displays the WS-Management identity schema, the protocol version, the product vendor, and the product version of the tested service.

    # Get parameters, examples, full and Online help for a cmdlet or function

    (Get-Command -Name Import-Csv).Parameters
    Get-help -Name Import-Csv -Examples
    Get-help -Name Import-Csv -Full
    Get-help -Name Import-Csv -Online

    Import-Csv -Path 'D:\Lists\Servers.csv' -Delimiter ' '

    (Get-Command -Name Test-WSMan).Parameters
    Get-help -Name Test-WSMan -Examples
    Get-help -Name Test-WSMan -Full
    Get-help -Name Test-WSMan -Online

    Test-WSMan -ComputerName 'ServerName' -Verbose

    Get-Help about_*
    Get-Help about_Functions

    # Find all cmdlets / functions with a target parameter
    Get-Help * -Parameter Append

    # All Help topics locations
    explorer "$pshome\$($Host.CurrentCulture.Name)"

So, someting like...

    ($ServerList = Import-csv -Path D:\Temp\DevSvrs.csv -Delimiter ' ')

    ServerName      : CTDEV02
    DNS             : ctdev02.dev.contoso.com
    Name            : poweredOn
    Powerstate      : Microsoft
    OperatingSystem : Windows

    ServerName      : tedev02
    DNS             : tedev02.dev.contoso.com
    Name            : poweredOn
    Powerstate      : Microsoft
    OperatingSystem : Windows

    ServerName      : CatDev02
    DNS             : catdev02.dev.contoso.com
    Name            : poweredOn
    Powerstate      : Microsoft
    OperatingSystem : Windows

    ServerName      : BDEV02
    DNS             : bdev02.dev.contoso.com
    Name            : poweredOn
    Powerstate      : Microsoft
    OperatingSystem : Windows

    ServerName      : web01
    DNS             : WEB01.dev.contoso.com
    Name            : poweredOn
    Powerstate      : Microsoft
    OperatingSystem : Windows


    $ServerList | %{
    If(Test-WSMan -ComputerName $_.ServerName -Verbose)
    {
        Get-Service -ComputerName $_ | 
        Select Name, DisplayName, Status, StartMode, StartType | Where-Object Name -eq 'WinRM'}
    }
    Else
    {Write-Warning -Message "WinRM test failed for $($_.ServerName)"}

March 7, 2018 at 9:10 pm

Sorry about that and thank you for pointing all that out.

March 7, 2018 at 9:19 pm

No worries.
It happens ever so often, and I ping folks back as soon I see it.
As you can see in my response, I sanitized that before posting.
Yet, who knows who also it before I hit you back.
So, it may turn out to be nothing for you and you org, but I'd keep an eye for any odd search's / requests to those FQDN's.