######################################################## ## ## Custom_GetAdminPW.ps1 ## ## Author: Thomas Kurth/Netree ## Date: 12.12.2013 ## ## History ## 001: First Version ## 002: Asymetric Encryption ## ######################################################## ## Manual Variable Definition ######################################################## $Debug = $false $ErrorActionPreference = "Stop" $privatekey = "xFrv7zv0DsyZSXh+kAeYuKWc5qI1q5V36iV96lk4It+5RnET0JiJ30J3fvEKJKlwm+1D40SqlcMTz+RPwklygnoa4oa+DcZp0fX2hILefdMiSlPMWrxvOw5rkWAqvDZSswOwUeL 2nr5feh2TYHKrivx8uckXSO5gwxWbIO6Sgw8=AQAB

8zNEYvX/qHvP+ZGKgrDYXv3KC1u8ktGCTVsCjsSiDJz5vWOM1X3pGnxqWpIsqMx7hF22sg/JjvlvPBXhCn n8Pw==

zrCCKq+bdm7N+eCZBsAuksXc2PMOiT1Jtb8fJ4DvpW/Y6q76dBUetuDl/6PLZmxeDOr+2F7bBYFTheysEbQFMQ==Dr1Ys7a9G/UFEdckUe9t7bPr/uw8bZmNECsn6Ic6WxFYgYX7 DyVmb1iDCaI6WrCp+8fCPuB+EtBDUZzx2rSx+Q==FYCjahFu2PlaEF7fTKZ9seNHwII4xupLWs8RTKjLRajHJlMh1yXDVj87bkb6CCPJ/QoNjTEBFCeKKuqeVBEoYQ==6YuS9 oYtuAi6OzUlQ8fxM1m7WSu553t+6OTplkeOS16lCJ5E1+A6Fp+Lz+AdcKOgIXkycpdR6X4GprFQN0rQJQ==N0nZ1JWfl4kxwlfM31ZcQOXVI7XaAenw3XvYFCQyI9O2Eikl6K8C51wKVLKf ZgpV3Hn3CyfbFpda3UdO1AilVqxITDacFTCnpyfsSXgCRB6abM+oXchNTwczU4tVrV6YZF+3swP04Fr1KHhLtWiHM4DZoTZZewmbM01WoYz04vE=
" $serverfqdn = "SERVERNAME_FQDN" # Servername where the netECM:MiniWebService is installed $LogFilePath = "C:\ProgramData\Netree\netECM2012 Manager Service\Logs\Custom_GetAdminPW.log" $LogFilePathScriptName = "Custom_GetAdminPW.ps1" # This is only used if the filename could not be resolved(IE running in ISE) $LogFilePathScriptPath = "C:\ProgramData\Netree\netECM2012 Manager Service\Logs" # This is only used if the filename could not be resolved(IE running in ISE) ## Functions ######################################################## #Write text to a logfile with the current time. function WriteLog { param( [Parameter(Mandatory=$True,Position=1,ValueFromPipeline=$true, HelpMessage='Text to write to log file.')] [string]$Text ) Out-file -FilePath $LogFilePath -force -append -InputObject ((Get-Date –f o) + " " + $Text) Write-Host $Text } ################# # Powershell Allows The Loading of .NET Assemblies # Load the Security assembly to use with this script ################# [Reflection.Assembly]::LoadWithPartialName("System.Security") > $null ################# # Powershell Allows The Loading of .NET Assemblies # Load the Json assembly to use with this script ################# [Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions") > $null $ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer function Decrypt-String($EncryptedString, $privatekey) { # If the value in the Encrypted is a string, convert it to Base64 $Encrypted = [Convert]::FromBase64String($EncryptedString) # Load the RSA Crypto Service Provider $RSAPROV = New-Object -TypeName 'System.Security.Cryptography.RSACryptoServiceProvider' $RSAPROV.FromXmlString($privatekey) # Decrypt $data = $RSAPROV.Decrypt($Encrypted,$false) # Transform String to UTF8 Byte array $enc = [system.Text.Encoding]::UTF8 $String = $enc.GetString($data) return $String } ## Initialization ######################################################## WriteLog "Start Script $Scriptname" ## Main Script ######################################################## try{ #Get Computername and Username if ($args.length -eq 2) { $computername = $args[0] $username = $args[1] } else { throw "No computername($computername) or UserName($username) is specified as argument!" } WriteLog "Computer: $computername" WriteLog "Username: $username" #Check Connection to Webservice $uri = "http://$serverfqdn/netECMMiniWebService/TSClient.svc" $prx = New-WebServiceProxy -uri $uri if($prx -eq $null){ throw "No connection to Web Service" } WriteLog "Connected to Web Service" #Get encrypted Password from Webservice $jsonObj = $prx.GetPropStoreLastVar($computername,"password") WriteLog "Encrypted Password loaded" if($jsonObj -eq $null){ WriteLog "No password for $computername found!!! Try the old one." } else { #Deserialize Object $EncryptedPasswordObj = $ser.DeserializeObject($jsonObj) $EncryptedPassword = $EncryptedPasswordObj["value"] # WriteLog "Encrypted PW: $EncryptedPassword" $UnEncryptedPassword = Decrypt-String -Encrypted $EncryptedPassword -privatekey $privatekey WriteLog "Unencrypted PW: $UnEncryptedPassword" $setDate = $EncryptedPasswordObj["date"] WriteLog "This password was set on: $setDate" } } catch { WriteLog "Error: $($_.Exception)" } ## Finishing ######################################################## WriteLog "End Script $Scriptname"