Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Add-Title([string] $name) {
- "Mr. $name"
- }
- function Say-HelloWorld([string] $name = "World") {
- $computerName = $env:COMPUTERNAME
- $greeting = Add-Title($name)
- Write-Host "Hello, $greeting ($computerName)"
- }
- . ./functions.ps1
- $remoteUsername = "username"
- $remotePassword = "password"
- $remoteHostname = "172.16.0.100"
- $securePassword = ConvertTo-SecureString -AsPlainText -Force $remotePassword
- $cred = New-Object System.Management.Automation.PSCredential $remoteUsername, $securePassword
- Say-HelloWorld("Spolsky")
- Invoke-Command -computerName $remoteHostname -Credential $cred -ScriptBlock {
- Say-HelloWorld("Spolsky")
- }
- Invoke-Command -computerName $remoteHostname -Credential $cred -ScriptBlock ${function:Add-Title } -argumentlist "Spolsky"
- New-PSSession SERVER1 -Cred $creds | Enter-PSSession
- . \tsclientccommon.ps1
- . \Server1sharecommon.ps1
- function Get-ScriptDirectory
- {
- return Split-Path $script:MyInvocation.MyCommand.Path
- }
- function GetLocalFileName
- {
- param([string]$filename)
- return [system.io.path]::Combine((Get-ScriptDirectory),$filename)
- }
- # dot-source the library using full local name to get round problem with remote invocation
- $MYLIBRARY = GetLocalFileName "mylibrary.ps1"
- . $MYLIBRARY
- Import-Module .Common.ps1 -Force
- # Now you can call common functions locally
- $commonFunctions = (Get-Command .Common.ps1).ScriptContents
- Invoke-Command -Session $session -ArgumentList $commonFunctions -ScriptBlock {
- param($commonFunctions)
- Invoke-Expression $commonFunctions
- # Now you can call common functions on the remote computer
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement