Created
May 6, 2017 20:03
-
-
Save Cyreex/b96893b1c0466e057c413bfcef7eb2e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$VmmServerName = "VMM01" | |
$SQLserver = "VMMDB" | |
$SQLDatabase = "IploggingDB" | |
$SQLuser = "MyUser" | |
$SQLPassword = "SecretPWD" | |
# Import VMM module. | |
Import-Module virtualmachinemanager | |
# Connect to VMM server. | |
Get-SCVMMServer -ComputerName $VmmServerName -ForOnBehalfOf | |
$Operation = "Create" | |
$date = get-date | |
$Date = '{0:s}' -f $date | |
$Name = "Manual" | |
### MsSQL CONNECTION | |
$Connection = New-Object System.Data.SQLClient.SQLConnection | |
$Connection.ConnectionString = "Server = '$SQLServer';database='$SQLDatabase'; User ID = '$SQLuser'; ` | |
Password = '$SQLPassword';trusted_connection=true; Integrated Security=false" | |
$Connection.Open() | |
$Command = New-Object System.Data.SQLClient.SQLCommand | |
$Command.Connection = $Connection | |
### | |
$VMnetworks = Get-SCVMNetwork | |
foreach ($VMnetwork in $VMnetworks) { | |
$owner = $VMnetwork.Owner | |
if ($owner) { | |
$Gateways = Get-SCVMNetworkGateway -VMNetwork $VMnetwork | |
foreach ($Gateway in $Gateways) { | |
$IPs = ((Get-SCNATConnection -VMNetworkGateway $Gateway).Rules | ? ExternalPort -eq 0).Name | |
foreach ($IP in $IPs) { | |
$Command.CommandText = "INSERT INTO iplog (Owner, Operation, IP, Date, Name) ` | |
VALUES ('{0}','{1}','{2}','{3}','{4}')" ` | |
-f $Owner, $Operation, $IP, $Date, $Name | |
$Command.ExecuteNonQuery() | out-null | |
} | |
} | |
} | |
} | |
$Connection.Close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment