From 19ed12352baafa23d45b69ea0df958f4b89ae7c7 Mon Sep 17 00:00:00 2001 From: Floris van Enter Date: Fri, 20 Apr 2018 14:11:45 +0200 Subject: [PATCH] Added function_Invoke-RemoteScript.ps1 --- PowerShell/function_Invoke-RemoteScript.ps1 | 64 +++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 PowerShell/function_Invoke-RemoteScript.ps1 diff --git a/PowerShell/function_Invoke-RemoteScript.ps1 b/PowerShell/function_Invoke-RemoteScript.ps1 new file mode 100644 index 0000000..6a8eb8f --- /dev/null +++ b/PowerShell/function_Invoke-RemoteScript.ps1 @@ -0,0 +1,64 @@ +function Invoke-RemoteScript() { + <# + .SYNOPSIS + Runs a local script on one or more remote machines + .DESCRIPTION + This function connects via PSRemoting to a server to run a script saved locally. + Written by Floris van Enter | EnterMI + .PARAMETER ComputerName + The name of the computer(s) to query. + .PARAMETER Scriptfile + The name of the script to run (with path) + .PARAMETER Username + Optional. The name of the user to remote connect + .PARAMETER Password + Optional. The password of the user to remote connect + .EXAMPLE + Invoke-RemoteScript -ComputerName computerA -Username "AD\AA99BB" -Password "bla bla ww" -Scriptfile ".\test.ps1" + .EXAMPLE + Invoke-RemoteScript -ComputerName computerA,computerB,computerC -Scriptfile "c:\temp\test.ps1" + .LINK + https://www.entermi.nl + #> + + [CmdletBinding()] + Param( + [Parameter( Mandatory=$True, + ValueFromPipeline=$True)] + [string[]]$ComputerName, + [Parameter(Mandatory=$True, + ValueFromPipeline=$True)] + [string]$Scriptfile, + [Parameter(Mandatory=$True, + ValueFromPipeline=$True)] + [string]$Username, + [Parameter(Mandatory=$True, + ValueFromPipeline=$True)] + [string]$Password + ) + + # Get the correct credential and use it for the computers + if ($PSBoundParameters.ContainsKey('Username') -and $PSBoundParameters.ContainsKey('Password') ) { + Write-Verbose "Convert password to SecureString for user $Username and save it as an Credential" + $pwdString = ConvertTo-SecureString -String $Password -AsPlainText -Force + $credentials = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $pwdString + } elseif ($PSBoundParameters.ContainsKey('Username')) { + Write-Verbose "Ask password for $Username and save it as an Credential" + $credentials = Get-Credential -UserName $Username -Message "Enter the password for user: $username" + } else { + Write-Verbose "Run the remote script as logged in user: $env:username" + } + + # Get the correct credential and use it for the computers + Foreach($computer in $ComputerName) { + Write-Verbose "Connect with $computer" + if(Test-Path Variable:\Credentials) { + Write-Verbose "Connect with $credentials.Username" + Invoke-Command -ComputerName $Computer -FilePath $Scriptfile + } else { + Write-Verbose "Connect with Logged On User" + Invoke-Command -ComputerName $Computer -FilePath $Scriptfile -Credential $credentials + } + } + Remove-Variable -Name Credentials +} \ No newline at end of file