简体   繁体   中英

Running PowerShell script locally from remote location

I am having an issue that I can't resolve. I am trying to run a script locally on a server, the script exists on a remote server. It is a script that I created that makes a series of domain credentials from a file with secure strings and a key file.

The script works fine locally if the ps1 file itself is on the system where I am running it. I am unable to run the script locally if the ps1 file is on the other machine.

Here is the script that I am trying to run... (this ps1 file is on "server1")

$creds = Import-Csv "\\server1\D$\Credentials\creds.csv"
$key = Get-Content "\\server1\D$\Resources\AES.key"
foreach ($cred in $creds) {
    $user = $cred.User
    $password = $cred.Hash | ConvertTo-SecureString -Key $key
    #$password = "" | ConvertTo-SecureString -AsPlainText -Force
    $i = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$password
New-Variable -Name ($cred.Domain + "_Cred") -Value $i -Force -Verbose
}

I am trying to run it from server2 using the following command...

& '\\server1\D$\Credentials\BuildCreds.ps1'

Any thoughts? Is this a scope issue, or the fact that I am loading the files with UNC? I've tried everything that I can think of.

It seems like this issue only exists in PowerShell ISE. I ran the code in PowerShell and everything worked correctly.

I am not quite sure why it is like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM