Script is looking for a Get-Content file path on remote server

Welcome Forums General PowerShell Q&A Script is looking for a Get-Content file path on remote server

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #144821

    Participant
    Topics: 4
    Replies: 3
    Points: 79
    Rank: Member

    Serverlist.txt content (localhost and RemoteServer

    When I run this on localhost it populates the -RemoteAddress from the $ips variable but it looks for ServerList.txt in the RemoteServer local path.

    1. How can I make it look for for ServerList.txt in localhost?
    2. I have c:\users\username\... how can i change this to an environment variable so that it will look in the $PSModulePath of the current use

    Her is my script (RE-purposed)

    
    Begin {$ErrorActionPreference = 'SilentlyContinue'
    $Servers = New-PSSession -ComputerName (Get-Content -path 'C:\Users\username\Documents\WindowsPowerShell\modules\SetIPs\ServerList.txt') -Credential $Credential
    Get-PSSession | Select-Object -Property ComputerName,State,Availability,ComputerType | Format-Table -AutoSize
    $User = Read-Host 'Type "READY" to countinue'
    Write-Host "Updating 2 exception rules IP Address Scope"
    }#Begin
    
    Process{Invoke-Command -Session $Servers -ScriptBlock {
    $ips = Get-Content -Path 'C:\Users\username\Documents\WindowsPowerShell\modules\SetIPs\IPList.txt'
    $names = Get-NetFirewallRule -Name 'SMB/RPC exception*'
    foreach ($name in $names)
    {Set-NetFirewallRule -Name 'SMB/RPC exception*' -RemoteAddress $ips
    }
    }#invoke
    
    }#Process
    
    End{
    Get-NetFirewallRule -Name 'SMB/RPC exceptions*' | Select -Property Name,Enabled,Direction
    Get-PSSession | Remove-PSSession
    }#End
    
    Export-ModuleMember -Variable Servers,user,ExampleErrorLogFile
    
    
  • #144899

    Participant
    Topics: 1
    Replies: 1331
    Points: 1,680
    Helping Hand
    Rank: Community Hero

    Please format your code as code using the code tag button right above the edit windows of the post editor. You can edit your already existing post and fix this.

    Thanks in advance.

  • #144951

    Participant
    Topics: 2
    Replies: 937
    Points: 1,628
    Helping Hand
    Rank: Community Hero

    Dynamically getting local use profile.

    # Either use

    $env:USERPROFILE

    # or

    $HOME

    If you mean some file on your system that you want to use on a remote host, then it has to be scoped. See the documentation.

    you could use local variables in remote PowerShell sessions by passing arguments to the remote scriptblock and capturing them inside the scriptblock with either the $args automatic variable or param() blocks. But since Windows PowerShell v3, we can use the scope modifier $using to change the scope of local variables. This makes them work in the remote session very easily without passing any arguments and capturing the arguments or parameters.

    You can use them with Invoke-Command and New-PSSession:

    1.The $args automatic variable
    2.The $using scope modifier
    3.The param() block

    About Remote Variables

    SHORT DESCRIPTION
    Explains how to use local and remote variables in remote commands.

    LONG DESCRIPTION
    You can use variables in commands that you run on remote computers. Simply assign a value to the variable and then use the variable in place of the value.

    By default, the variables in remote commands are assumed to be defined in the session in which the command runs. You can also use variables that are defined in the local session, but you must identify them as local variables in the command.

    USING LOCAL VARIABLES
    You can also use local variables in remote commands, but you must indicate that the variable is defined in the local session.
    Beginning in Windows PowerShell 3.0, you can use the Using scope modifier to identify a local variable in a remote command.

    The syntax of Using is as follows:

    $Using:
    

    In the following example, the $ps variable is created in the local session, but is used in the session in which the command runs. The Using scope modifier identifies $ps as a local variable.

    $ps = "Windows PowerShell"
    Invoke-Command -ComputerName S1 -ScriptBlock {
      Get-WinEvent -LogName $Using:ps
    }
    

    You can also use the Using scope modifier in PSSessions.

    $s = New-PSSession -ComputerName S1
    $ps = "Windows PowerShell"
    Invoke-Command -Session $s -ScriptBlock {Get-WinEvent -LogName $Using:ps}
    
  • #144959

    Participant
    Topics: 1
    Replies: 1331
    Points: 1,680
    Helping Hand
    Rank: Community Hero

    If I understood what you're trying to do you don't need a proces block. Invoke-Command should be able do deal with multiple sessions by itself. The script block inside Invoke-Command runs on the remote machine. Therefor the path inside this script block points to the remote machines path. You can populate the content of the IPList file outside the script block and use the variable inside. This should work actually

    $ServerList = Get-Content -path 'C:\Users\username\Documents\WindowsPowerShell\modules\SetIPs\ServerList.txt'
    $Servers = New-PSSession -ComputerName $ServerList -Credential $Credential
    Get-PSSession | 
        Select-Object -Property ComputerName,State,Availability,ComputerType | 
            Format-Table -AutoSize
    Write-Host "Updating 2 exception rules IP Address Scope"
    
    $ips = Get-Content -Path 'C:\Users\username\Documents\WindowsPowerShell\modules\SetIPs\IPList.txt'
    Invoke-Command -Session $Servers -ScriptBlock {
        $names = Get-NetFirewallRule -Name 'SMB/RPC exception*'
        foreach ($name in $names) {
            Set-NetFirewallRule -Name 'SMB/RPC exception*' -RemoteAddress $USING:ips
        }
    }#invoke
    
    
    Get-NetFirewallRule -Name 'SMB/RPC exceptions*' | Select-Object -Property Name,Enabled,Direction
    Get-PSSession | Remove-PSSession
    
    Export-ModuleMember -Variable Servers,user,ExampleErrorLogFile

    ... untested

    BTW: formating code means to indent code accordingly as well! 😉

  • #145029

    Participant
    Topics: 4
    Replies: 3
    Points: 79
    Rank: Member

    Thanks @Olof and @postanote for you response. It has been of tremendous help. Issue was resolved.

  • #145049

    Participant
    Topics: 1
    Replies: 1331
    Points: 1,680
    Helping Hand
    Rank: Community Hero

    I'm glad to be of any help. You might share your solution to help other having the same or a similar problem. 😉

You must be logged in to reply to this topic.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort