Execution of *.ps1 files by doubleclicking is forbidden on default.
There are sevaral ways to still execute Scripts, e.g. by starting a powerShell with the script as command parameter
There is a detailed describtion of that https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/
The scope in which powershell variables are visible: http://technet.microsoft.com/en-us/library/hh847849.aspx
Get-ExecutionPolicy -List | Format-Table -AutoSize
The execution policies you can use are:
Set-ExecutionPolicy <policy name>
Remote execution of Scripts via powershell is described here: https://www.opswat.com/blog/powershell-vs-psexec-remote-command-execution
On the remote machine, enable remote commands
PS C:\Windows\system32> Enable-PSRemoting -force
On a local machine, add remote system to the trusted list
C:\Windows\system32> Set-Item wsman:\localhost\Client\TrustedHosts -value 10.0.X.X
On a local machine, set the execution policy to remote signed
PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned
Execute Scripts on the remote machine
PS C:\Windows\system32> invoke-command -computername YOURNAMEHERE -scriptblock {echo "Test" > D:\tmp\1Remote\testRemote.txt}
Execute Script file on remote pc and log to *.txt
PS C:\Windows\system32> invoke-command -computername ANB13010 -scriptblock {powershell -File D:\script.ps1 > D:\logs.txt}
Executing with autehntification:
$username = 'your-domain.com\username' $password = 'yourpassword' $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force)) invoke-command -computername ANB13010 -Credential $cred -Authentication CredSSP -scriptblock {powershell -File D:\script.ps1 > D:\logs.txt} | Write-Host
As stated here: http://stackoverflow.com/questions/6178437/start-remote-process-within-the-context
You cannot start interactive processes using WMI or PowerSHell remoting. This is a security limitation/feature. You need to use PSExec if you want to start remote interactive processes.
You can use PSExec tool for that, available here http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
# WORKS! $remoteMachine = 'PCNAMEHERE' $username = 'your.domain.com\usernamehere' $password = 'passwordhere' D:\Temp\22symantec\PsExec.exe \\$remoteMachine -u $username -p $password /accepteula -d cmd /c "powershell -noninteractive D:\path\to\script\scriptname.ps1"
Just write the .exe down with the parameters or use the call operator &
Details are here http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
When executing commands on foreign computer B - B will not be able to access another computer C via Network.
This happens because B can not pass the creadentials further to another PCs on default.
You can bypass this problem by giving B this ability.
#Erlauben die Powershell Skripte auszufuhren Set-ExecutionPolicy RemoteSigned #workaround second-hop um auf Netzfreigaben zu zugreifen http://technet.microsoft.com/en-us/magazine/jj853299.aspx Enable-WSManCredSSP –Role client –DelegateComputer * -force # trust the deployment machine Set-Item wsman:\localhost\Client\TrustedHosts -value ANB13010 Set-Item wsman:\localhost\Client\TrustedHosts -value 192.168.51.116
#Erlauben die Powershell Skripte auszufuhren Set-ExecutionPolicy RemoteSigned #erlaube den remote Zugriff via Skripte Enable-PSRemoting -force #workaround second-hop um auf Netzfreigaben zu zugreifen http://technet.microsoft.com/en-us/magazine/jj853299.aspx Enable-WSManCredSSP –Role server -force #fuge die BuildSlaves zu TrustedHosts hinzu Set-Item wsman:\localhost\Client\TrustedHosts -value a-pc-p31dash01 Set-Item wsman:\localhost\Client\TrustedHosts -value 192.168.51.116
You can invoke commands on foreign PCs as following. The following command can be called from PC A
to be executed on PC B with name ANB13010
in order to list files on network share on PC C with ip 192.168.51.116
$username = 'my-domain.com\username' $password = 'password' $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force)) invoke-command -computername ANB13010 -Credential $cred -Authentication CredSSP -scriptblock { Get-ChildItem -Path \\192.168.51.116\networkShare | echo}
(Get-Content c:\temp\replace\tnsnames.txt) ` -replace 'HOST=.*?\)', 'HOST=MyHost)' | Out-File c:\temp\replace\tnsnames.txt