Invoke-Command [$Session] Variable is Null or Empty, Why?

Hello,


I am posting this question to see if anyone can help me understand the reason why the $Session variable I have created is coming up null or empty. I am still trying to learn PowerShell so I am not an expert at this, but the following is how I have written my script:

Function Get-RemoteInterfaceIP {

[CmdletBinding()]
Param(
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(ParameterSetName="Version4")]
[switch]$IPv4,
[Parameter(ParameterSetName="Version6")]
[switch]$IPv6

) #End of Param


Switch ($PSCmdlet.ParameterSetName) {

"Version4" {

ForEach($Computer in $ComputerName) {

If((Test-Connection -ComputerName $Computer -Count 1 -Quiet -ErrorAction SilentlyContinue) -eq "True") {

$Session = New-PSSession -ComputerName $Computer

Write-Host "$($Computer.ToUpper()) is online - $(Get-Date)" -ForegroundColor Green

Invoke-Command -Session $Session -ScriptBlock {

Get-NetIPAddress | Select-Object -Property IPAddress, @{Name ="Interface Name"; Expression={$_.InterfaceAlias}},`
@{Name = "AddressFamily"; Expresion={ ? {$_.AdressFamily -eq "IPv4"}}} | Sort-Object -Property "Interface Name"

} #End of Invoke-Command

} #End of IF

Else {

Write-Host "$Computer is offline - $(Get-Date)" -ForegroundColor Red

} #End of ELSE

Remove-PSSession -Session $CimSession

} #End of FOREACH

} #End of Version4 Switch

} #End of SWITCH Statement


} #End of Function Get-RemoteInterfaceIP

==================================

Trying to apply IPv4 & IPv6 switches to this script as well just to test the "Version4" parameter set, and I came across this error:

------------------------------------------------------------------------------------------------

** Note: WinRM is running n my machine **

WinRM is already set up to receive requests on this computer.
WinRM is already set up for remote management on this computer.

------------------------------------------------------------------------------------------------

New-PSSession : [My ComputerName] Connecting to remote server  [My ComputerName] failed with the following
error message : The WinRM client sent a request to an HTTP server and got a response saying the
requested HTTP URL was not available. This is usually returned by a HTTP server that does not support
the WS-Management protocol. For more information, see the about_Remote_Troubleshooting Help topic.
At C:\Temp\Get-RemoteInterfaceIP_Function-Draft.ps1:26 char:32
+ $Session = New-PSSession -ComputerName $Computer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSe
ssion], PSRemotingTransportException
+ FullyQualifiedErrorId : URLNotAvailable,PSSessionOpenFailed
 [My ComputerName] is online - 03/28/2018 14:29:11
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty.
Provide an argument that is not null or empty, and then try the command again.
At C:\Temp\Get-RemoteInterfaceIP_Function-Draft.ps1:30 char:45
+ Invoke-Command -Session $Session -ScriptBlock {
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCo
mmandCommand

Remove-PSSession : Cannot bind argument to parameter 'Session' because it is null.
At C:\Temp\Get-RemoteInterfaceIP_Function-Draft.ps1:45 char:47
+ Remove-PSSession -Session $Session
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-PSSession], ParameterBindingValidationExceptio
n
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Com
mands.RemovePSSessionCommand

==================================

If anyone can assist in trying to get this working so it works for remote machines it would be greatly appreciated. I have even tried to create the $Cim variable found in this link:

https://www.petri.com/manage-network-adapters-powershell-addressing-configuration-info

and use the --CimSession parameter with no joy. Same results.

Parents
No Data
Reply
  • Whether you use WMI or CIM cmdlets, if you are not using a set of credentials that is in the remote machines local administrator group, you cannot do this and of course it does no matter if your system had PSRemoting enabled. What matters is that the remote host has PSRemoting enabled and properly configured for a PSRemoting use case.

    There are several commands you ca use with or without remoting enabled or local admin membership.

    Tip: Work Remotely with Windows PowerShell without using Remoting or WinRM
    Some cmdlets have a –ComputerName parameter that lets you work with a remote computer without using Windows PowerShell remoting. This means you can use the cmdlet on any computer that is running Windows PowerShell, even if the computer is not configured for Windows PowerShell remoting. These cmdlets include the following:
    'technet.microsoft.com/en-us/library/ff699046.aspx'

    The Invoke-* cmdlets and many other are not one of them. Yet, for what you seem to be after, the Invoke-* is not really needed. You could have done something like.

        (Get-ADComputer -Filter *).Name |
        %{
            Test-Connection -ComputerName $_ -Count 1
            $AdapterName = (Get-WmiObject -Class Win32_NetworkAdapter -ComputerName $_ -filter "AdapterTypeId=0").Name
        } |  Select -Property *,@{name = 'AdapterName';expression = {$AdapterName}} |
        Where IPV4Address -ne $null |
        Select Address,ProtocolAddress,AdapterName | Format-Table -AutoSize

        Address      ProtocolAddress AdapterName                                             
        -------      --------------- -----------                                             
        DC01     192.168.0.11    Network Device                                   
        EX01     192.168.2.12     Network Device #0                                
        SQL01    192.168.3.11    Network Device #0                                
        ...

    Secondly, that error is pretty specific, you are not providing anything to work with.

    This very forums offers several free eBooks on PowerShell (PoSH) learning. For PSRemoting, see:

    eBook: Secrets of PowerShell Remoting --- 'powershell.org/2012/08/06/ebook-secrets-of-powershell-remoting'

Children
No Data