Invoke-Command fails to perform most actions...

This topic contains 2 replies, has 2 voices, and was last updated by  Scott 5 months, 1 week ago.

  • Author
    Posts
  • #70331

    Scott
    Participant

    I've created a script that configures our servers based on passed parameters, and it runs locally without issue. When I try to perform an Invoke-Command, it fails completely. There are no double-hop scenarios here, to be clear.  Below is a snippet of code, some of the arguments have been redacted as has the argument list, but the issue is not with those.
    If you look you can see that I'm creating a session, and then doing an invoke-command with that session where I kick off my script and pass in the parameters.  However the script fails time and again.  Here is what fails:

    $NetAdapter = Get-NetIPConfiguration
    $ips = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $NetAdapter.InterfaceIndex
    Install-PackageProvider -Name NuGet
    Import-Module SqlPs
    

    Why are these failing? I want a script that I can run locally and it works, which I've created and it works great, and I want to be able to remotely invoke it...these don't seem like they should be things that don't work within a session yet they are.

    $PSSession = New-PSSession -ComputerName $($Server.ServerName) -EnableNetworkAccess -Credential $DomainCredentials
    And yes, C:\Install-SingleInstance.ps1 does exist on the box at C:\.
    Invoke-Command -Session $PSSession -ScriptBlock { cd C:\; .\Install-SingleInstance.ps1 }
    
  • #70348

    Daniel Krebs
    Moderator

    You actually don't need to have the script local on each box you're connecting to. Just keep it accessible to the admin/jump box or use an embedded ScriptBlock if your script stays short. Much easier for testing and reusability by other team members.

    Two options:

    Example – File on disk of the admin workstation/jump box:

    $PSSession = New-PSSession -ComputerName $($Server.ServerName) -EnableNetworkAccess -Credential $DomainCredentials
    Invoke-Command -Session $PSSession -FilePath C:\My\Install-SingleInstance.ps1
    

    $PSSession = New-PSSession -ComputerName $($Server.ServerName) -EnableNetworkAccess -Credential $DomainCredentials
    Invoke-Command -Session $PSSession -ScriptBlock {
    $NetAdapter = Get-NetIPConfiguration
    $ips = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $NetAdapter.InterfaceIndex
    Install-PackageProvider -Name NuGet
    Import-Module SqlPs
    }

    • #70367

      Scott
      Participant

      There are other files that get copied to the remote system, executables, etc, that are then kicked off inside of the script I'm invoking.

You must be logged in to reply to this topic.