Advertisement
Guest User

Untitled

a guest
May 6th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. workflow IP-Logging
  2. {
  3. param
  4. (
  5. [Parameter(Mandatory=$true)]
  6. [String] $VMMJOBID,
  7. [Parameter(Mandatory=$true)]
  8. [object] $NAME,
  9. [Parameter(Mandatory=$true)]
  10. [object] $OPERATION
  11. )
  12.  
  13. # Connection to access VMM server
  14. $VmmConnection = Get-AutomationConnection -Name 'VmmConnection'
  15. $VmmServerName = $VmmConnection.ComputerName
  16.  
  17. $SecurePassword = ConvertTo-SecureString -AsPlainText -String $VmmConnection.Password -Force
  18. $VmmCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $VmmConnection.Username, $SecurePassword
  19.  
  20. # Connection to access MsSQL server.
  21. $MsSQLCred = Get-AutomationPSCredential -Name 'MsSQL-iploggingDB'
  22. [string] $MsSQLLogin = $MsSQLCred.Username
  23. $MsSQLPassword = $MsSQLCred.Password
  24. [string] $MsSQLDatabase = Get-AutomationVariable -Name 'MsSQL-iplogging-Database'
  25. [string] $MsSQLServer = Get-AutomationVariable -Name 'MsSQL-iplogging-Server'
  26.  
  27. inlinescript {
  28.  
  29. # Import VMM module.
  30. Import-Module virtualmachinemanager
  31.  
  32. # Connect to VMM server.
  33. Get-SCVMMServer -ComputerName $Using:VmmServerName -ForOnBehalfOf
  34.  
  35. $job = Get-SCJob -ID $USING:vmmjobid
  36.  
  37. # Check Job Status
  38.  
  39. $JobStatus=$Job.Status
  40.  
  41. # Wait Until Task Completed
  42. while ($JobStatus -eq "Running") {
  43.  
  44. write-output "Start Sleep"
  45. start-sleep 3
  46. $JobStatus=$Job.Status
  47.  
  48. }
  49.  
  50. # Break if Job failed
  51. if ($JobStatus -eq "Failed") {
  52.  
  53. write-output "Job Failed!"
  54. write-output JOBid:$job.ID
  55. break
  56.  
  57. }
  58.  
  59. #Get IP
  60.  
  61. $jobfull = Get-SCJob -Job $job -full
  62.  
  63. if ($Using:Operation -eq "Delete") {
  64.  
  65. $IP = (($jobfull.AuditRecords).Previous | ? key -eq "IP address" | Select -First 1).Value
  66.  
  67. } else {
  68.  
  69. $IP = (($jobfull.AuditRecords).New | ? key -eq "IP address" | Select -First 1).Value
  70.  
  71. }
  72.  
  73. ### Get data
  74.  
  75. $Owner = $job.Owner
  76. $Operation = $Using:Operation
  77. $Date = '{0:s}' -f $job.EndTime
  78. $Name = $Using:name
  79.  
  80. ### Write to Database
  81.  
  82. ### VARS DB Settings
  83. $SQLserver = $USING:MsSQLServer
  84. $SQLDatabase = $USING:MsSQLDatabase
  85. $SQLuser = $USING:MsSQLLogin
  86. $SQLSecurePassword = $USING:MsSQLPassword
  87. # We need unsecure password to connect DB
  88. $SQLBSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SQLSecurePassword)
  89. $SQLUnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($SQLBSTR)
  90.  
  91. ### MsSQL CONNECTION
  92. $Connection = New-Object System.Data.SQLClient.SQLConnection
  93. $Connection.ConnectionString = "Server = '$SQLServer';database='$SQLDatabase'; User ID = '$SQLuser'; `
  94. Password = '$SQLUnsecurePassword';trusted_connection=true; Integrated Security=false"
  95. $Connection.Open()
  96. $Command = New-Object System.Data.SQLClient.SQLCommand
  97. $Command.Connection = $Connection
  98. ###
  99.  
  100. $Command.CommandText = "INSERT INTO iplog (Owner, Operation, IP, Date, Name) `
  101. VALUES ('{0}','{1}','{2}','{3}','{4}')" `
  102. -f $Owner, $Operation, $IP, $Date, $Name
  103.  
  104. $Command.ExecuteNonQuery() | out-null
  105.  
  106. $Connection.Close()
  107.  
  108.  
  109. } -PSComputerName $VmmServerName -PSCredential $VmmCredential
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement