/
common.ps1
115 lines (104 loc) · 4.14 KB
/
common.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function DisplayDiskInfo() {
Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName,
@{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } },
@{ Name = "Size (GB)" ; Expression = { "{0:N1}" -f ( $_.Size / 1gb) } },
@{ Name = "FreeSpace (GB)" ; Expression = { "{0:N1}" -f ( $_.Freespace / 1gb ) } },
@{ Name = "PercentFree" ; Expression = { "{0:P1}" -f ( $_.FreeSpace / $_.Size ) } } |
Format-Table -AutoSize | Out-String
}
function GetProductVersion ($partialName) {
$x64items = @(Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")
$x64items + @(Get-ChildItem "HKLM:SOFTWARE\wow6432node\Microsoft\Windows\CurrentVersion\Uninstall") `
| ForEach-object { Get-ItemProperty Microsoft.PowerShell.Core\Registry::$_ } `
| Where-Object { $_.DisplayName -and $_.DisplayName.contains($partialName) } `
| Sort-Object -Property DisplayName `
| Select-Object -Property DisplayName, DisplayVersion `
| Format-Table -AutoSize | Out-String
}
function Start-ProcessWithOutput {
param(
$command,
[switch]$ignoreExitCode,
[switch]$ignoreStdOut
)
$fileName = $command
$arguments = $null
if ($command.startsWith('"')) {
$idx = $command.indexOf('"', 1)
$fileName = $command.substring(1, $idx - 1)
if ($idx -lt ($command.length - 2)) {
$arguments = $command.substring($idx + 2)
}
}
else {
$idx = $command.indexOf(' ')
if ($idx -ne -1) {
$fileName = $command.substring(0, $idx)
$arguments = $command.substring($idx + 1)
}
}
# find tool in path
if (-not (Test-Path $fileName)) {
foreach ($pathPart in $($env:PATH).Split(';')) {
$searchPath = [IO.Path]::Combine($pathPart, "$fileName.bat")
if (Test-Path $searchPath) {
$fileName = $searchPath; break;
}
$searchPath = [IO.Path]::Combine($pathPart, "$fileName.cmd")
if (Test-Path $searchPath) {
$fileName = $searchPath; break;
}
$searchPath = [IO.Path]::Combine($pathPart, "$fileName.exe")
if (Test-Path $searchPath) {
$fileName = $searchPath; break;
}
$searchPath = [IO.Path]::Combine($pathPart, $fileName)
if (Test-Path $searchPath) {
$fileName = $searchPath; break;
}
}
}
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $fileName
$psi.RedirectStandardError = $true
$psi.RedirectStandardOutput = $true
$psi.CreateNoWindow = $true
$psi.UseShellExecute = $false
$psi.Arguments = $arguments
$psi.WorkingDirectory = (pwd).Path
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $psi
# Adding event handers for stdout and stderr.
$outScripBlock = {
if (![String]::IsNullOrEmpty($EventArgs.Data)) {
Write-Host "$($EventArgs.Data)"
}
}
$errScripBlock = {
if (![String]::IsNullOrEmpty($EventArgs.Data)) {
Write-Host "$($EventArgs.Data)" -ForegroundColor Red
}
}
if ($ignoreStdOut -eq $false) {
$stdOutEvent = Register-ObjectEvent -InputObject $process -Action $outScripBlock -EventName 'OutputDataReceived'
}
$stdErrEvent = Register-ObjectEvent -InputObject $process -Action $errScripBlock -EventName 'ErrorDataReceived'
try {
$process.Start() | Out-Null
$process.BeginOutputReadLine()
$process.BeginErrorReadLine()
[Void]$process.WaitForExit()
# Unregistering events to retrieve process output.
if ($ignoreStdOut -eq $false) {
Unregister-Event -SourceIdentifier $stdOutEvent.Name
}
Unregister-Event -SourceIdentifier $stdErrEvent.Name
if ($ignoreExitCode -eq $false -and $process.ExitCode -ne 0) {
exit $process.ExitCode
}
}
catch {
Write-Host "Error running '$($psi.FileName) $($psi.Arguments)' command: $($_.Exception.Message)" -ForegroundColor Red
throw $_
}
}