Advertisement
stephanlinke

[PRTG] Execute Command on Windows based target host

Oct 4th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param(
  2.     $computername = "<zielcomputer>",
  3.     $username     = "<domain>\<username>",
  4.     $password     = '<passwort>',
  5.     $command      = "<kommando inkl. parametern>"
  6. )
  7.  
  8. Import-Module @("Microsoft.PowerShell.Management","PSDiagnostics");
  9.  
  10. try{
  11.     # Generate credentials object for authentication
  12.     $SecPasswd   = ConvertTo-SecureString $password -AsPlainText -Force
  13.     $Credentials = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
  14.  
  15.     Invoke-Command -ComputerName $computername -ScriptBlock { param($command) Invoke-Expression $command } -ArgumentList @($command) -Credential ($Credentials) }
  16.  
  17. catch
  18. { throw [System.IO.FileNotFoundException] "The command could not be executed on the remote host"; exit 2 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement