Last active
June 21, 2017 16:46
-
-
Save mattmichal/c286641d63e72eb8c69eef4cc2bcd6b4 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
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true)] | |
[string] $hostname, | |
[Parameter(Mandatory=$true)] | |
[string] $username, | |
[Parameter(Mandatory=$true)] | |
[string] $password, | |
[Parameter(Mandatory=$true)] | |
[string] $sourcedirectory, | |
[Parameter(Mandatory=$true)] | |
[string] $targetdirectory, | |
[Parameter(Mandatory=$true)] | |
[string] $executablename | |
) | |
#Set-Variable PsExecPath -option Constant -value $Env:bamboo_capability_system_builder_command_PsExec_2_45_x64 | |
#Set-Variable PsListPath -option Constant -value $Env:bamboo_capability_system_builder_command_PsList_2_45_x64 | |
#Set-Variable PsKillPath -option Constant -value $Env:bamboo_capability_system_builder_command_PsKill_2_45_x64 | |
Set-Variable PsExecPath -Option Constant -Value "C:\Users\mattmichal\Desktop\PsTools\psexec64.exe" | |
Set-Variable PsListPath -Option Constant -Value "C:\Users\mattmichal\Desktop\PsTools\pslist64.exe" | |
Set-Variable PsKillPath -Option Constant -Value "C:\Users\mattmichal\Desktop\PsTools\pskill64.exe" | |
Set-Variable EchoArgsPath -Option Constant -Value "C:\Users\mattmichal\Desktop\PsTools\echoargs.exe" | |
function GetSessionIdOfUser | |
( | |
[string] $hostname, | |
[string] $username, | |
[string] $password | |
) | |
{ | |
# hangs: $psExecArgs = "-accepteula", "-nobanner", "`\`\${hostname}","-u domain`\${username}", "-p ${password}", "qwinsta" | |
$psExecArgs = "-accepteula", "-nobanner", "`\`\${hostname}", "qwinsta" | |
$result = & $PsExecPath $psExecArgs 2>$null | |
if ($LASTEXITCODE -ne 0) | |
{ | |
exit 1 | |
} | |
return $result | | |
Select-String "${username}\s+(\w+)" | | |
Foreach {$_.Matches[0].Groups[1].Value} | |
} | |
function StartExecutable | |
( | |
[string] $hostname, | |
[string] $username, | |
[string] $password, | |
[string] $sessionId, | |
[string] $executableDirectory, | |
[string] $executableName | |
) | |
{ | |
# invalid args: $psExecArgs = "-accepteula", "-nobanner", "-u domain`\${username}", "-p ${password}", "-h", "-i ${sessionId}", "-d", "`\`\${hostname}", "`"${executableDirectory}`\${executableName}`"" | |
# hangs: $psExecArgs = "-accepteula", "-nobanner", "-u domain`\${username}", "-p ${password}", "-h", "-d", "`\`\${hostname}", "`"${executableDirectory}`\${executableName}`"" | |
$psExecArgs = "-accepteula", "-nobanner", "-h", "-d", "`\`\${hostname}", "`"${executableDirectory}`\${executableName}`"" | |
& $EchoArgsPath $psExecArgs | |
$result = & $PsExecPath $psExecArgs #2>$null | |
if ($LASTEXITCODE -ne 0) | |
{ | |
exit 1 | |
} | |
} | |
function GetProcessIdWithTasklist | |
( | |
[string] $hostname, | |
[string] $username, | |
[string] $password, | |
[string] $executableName | |
) | |
{ | |
$result = tasklist /s $hostname /u "domain`\${username}" /p "$password" /fi "IMAGENAME eq ${executableName}" 2>$null | |
if ($LASTEXITCODE -ne 0) | |
{ | |
exit 1 | |
} | |
$imageName = $executableName[0..24] -join "" | |
return $result | | |
Select-String "^${imageName}\s+(\w+)" | | |
Foreach {$_.Matches[0].Groups[1].Value} | |
} | |
function GetProcessIdWithPslist | |
( | |
[string] $hostname, | |
[string] $username, | |
[string] $password, | |
[string] $executableName | |
) | |
{ | |
$processName = [System.IO.Path]::GetFileNameWithoutExtension($executableName) | |
# hangs: $psListArgs = "-accepteula", "-nobanner", "\\${hostname}", "-u domain`\${username}", "-p ${password}", "${processName}" | |
$psListArgs = "/accepteula", "-nobanner", "\\${hostname}", "${processName}" | |
$result = & $PsListPath $psListArgs 2>$null | |
if ($LASTEXITCODE -ne 0) | |
{ | |
exit 1 | |
} | |
return $result | | |
Select-String "^${processName}\s+(\w+)" | | |
Foreach {$_.Matches[0].Groups[1].Value} | |
} | |
function StopExecutableByIdWithTaskkill | |
( | |
[string] $hostname, | |
[string] $username, | |
[string] $password, | |
[string] $processId | |
) | |
{ | |
taskkill /s $hostname /u "domain`\${username}" /p "$password" /fi "PID eq ${processId}" 2>$null | |
if ($LASTEXITCODE -ne 0) | |
{ | |
exit 1 | |
} | |
} | |
function StopExecutableByIdWithPskill | |
( | |
[string] $hostname, | |
[string] $username, | |
[string] $password, | |
[string] $processId | |
) | |
{ | |
# hangs: $psKillArgs = "-accepteula", "-nobanner", "\\${hostname}", "-u domain`\${username}", "-p ${password}", "${processId}" | |
$psKillArgs = "-accepteula", "-nobanner", "\\${hostname}", "${processId}" | |
$result = & $PsKillPath $psKillArgs 2>$null | |
if ($LASTEXITCODE -ne 0) | |
{ | |
exit 1 | |
} | |
} | |
function StopExecutableByNameWithPskill | |
( | |
[string] $hostname, | |
[string] $username, | |
[string] $password, | |
[string] $executableName | |
) | |
{ | |
$processName = [System.IO.Path]::GetFileNameWithoutExtension($executableName) | |
$psKillArgs = "-accepteula", "\\${hostname}", "-u domain`\${username}", "-p ${password}", "${processName}" | |
$result = & $PsKillPath $psKillArgs #2>$null | |
if ($LASTEXITCODE -ne 0) | |
{ | |
exit 1 | |
} | |
} | |
function ConvertLocalPathToUncPath([string] $localPath, [string] $hostname) | |
{ | |
return "`\`\$hostname`\$($localPath.Replace(':','$'))" | |
} | |
function CopyFiles([string] $from, [string] $to) | |
{ | |
Get-ChildItem "${from}*" -Recurse | Copy-Item -Destination $to | |
} | |
function UpdateRunningExecutable | |
( | |
[string] $hostname, | |
[string] $username, | |
[string] $password, | |
[string] $sourceDirectory, | |
[string] $targetDirectory, | |
[string] $executableName | |
) | |
{ | |
$userSessionId = GetSessionIdOfUser $hostname $username | |
if ([string]::IsNullOrEmpty($userSessionId)) | |
{ | |
# user must have an interactive session started already | |
exit 1 | |
} | |
$processId = GetProcessIdWithTasklist $hostname $username $password $executableName | |
if (![string]::IsNullOrEmpty($processId)) | |
{ | |
StopExecutableByIdWithTaskkill $hostname $username $password $processId | |
} | |
$targetUncPath = ConvertLocalPathToUncPath $targetDirectory $hostname | |
CopyFiles $sourceDirectory $targetUncPath | |
StartExecutable $hostname $username $password $userSessionId $targetDirectory $executableName | |
} | |
UpdateRunningExecutable $hostname $username $password $sourcedirectory $targetdirectory $executablename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment