Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. function Add-Title([string] $name) {
  2. "Mr. $name"
  3. }
  4.  
  5. function Say-HelloWorld([string] $name = "World") {
  6. $computerName = $env:COMPUTERNAME
  7. $greeting = Add-Title($name)
  8. Write-Host "Hello, $greeting ($computerName)"
  9. }
  10.  
  11. . ./functions.ps1
  12.  
  13.  
  14. $remoteUsername = "username"
  15. $remotePassword = "password"
  16. $remoteHostname = "172.16.0.100"
  17.  
  18. $securePassword = ConvertTo-SecureString -AsPlainText -Force $remotePassword
  19. $cred = New-Object System.Management.Automation.PSCredential $remoteUsername, $securePassword
  20.  
  21.  
  22. Say-HelloWorld("Spolsky")
  23.  
  24. Invoke-Command -computerName $remoteHostname -Credential $cred -ScriptBlock {
  25. Say-HelloWorld("Spolsky")
  26. }
  27.  
  28. Invoke-Command -computerName $remoteHostname -Credential $cred -ScriptBlock ${function:Add-Title } -argumentlist "Spolsky"
  29.  
  30. New-PSSession SERVER1 -Cred $creds | Enter-PSSession
  31. . \tsclientccommon.ps1
  32.  
  33. . \Server1sharecommon.ps1
  34.  
  35. function Get-ScriptDirectory
  36. {
  37. return Split-Path $script:MyInvocation.MyCommand.Path
  38. }
  39.  
  40. function GetLocalFileName
  41. {
  42. param([string]$filename)
  43. return [system.io.path]::Combine((Get-ScriptDirectory),$filename)
  44. }
  45.  
  46. # dot-source the library using full local name to get round problem with remote invocation
  47. $MYLIBRARY = GetLocalFileName "mylibrary.ps1"
  48. . $MYLIBRARY
  49.  
  50. Import-Module .Common.ps1 -Force
  51. # Now you can call common functions locally
  52.  
  53. $commonFunctions = (Get-Command .Common.ps1).ScriptContents
  54. Invoke-Command -Session $session -ArgumentList $commonFunctions -ScriptBlock {
  55. param($commonFunctions)
  56. Invoke-Expression $commonFunctions
  57.  
  58. # Now you can call common functions on the remote computer
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement