-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiis-server-info-collector-runner.ps1
74 lines (59 loc) · 2.49 KB
/
iis-server-info-collector-runner.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
$PsVersion = "PowerShell Version: " + $PSVersionTable.PSVersion
Write-Output ""
Write-Output $PsVersion
$Policy=Get-ExecutionPolicy
Write-Output "Execution Policy for current session: $Policy"
Write-Output ""
$Computer
$OutputPath = "c:\iis-info"
$ExecutingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$ScriptPath = Join-Path $ExecutingScriptDirectory "iis-server-info-collector.ps1"
if (Test-Path $OutputPath) {
Remove-Item $OutputPath -Force -Recurse
}
New-Item $OutputPath -ItemType Directory | Out-Null
if ($args -eq $null -or $args.count -eq 0) {
Write-Output "Running the script against localhost computer..."
Write-Output ""
& $ScriptPath
} else {
$IsInSameDomain = Read-Host -Prompt "Are you running the script on the machine that joined the same domain as remote servers? (y/n)"
$RequireCredential
if ($IsInSameDomain -eq "y") {
$RequireCredential = $false
} else {
$RequireCredential = $true
Write-Output ""
Write-Output "Running the script on the machine lives in different domain of remote servers"
Write-Output "requires username and password for login to remote servers."
Write-Output ""
Write-Output "Please note that, the user must be in Administrator group. Press Enter to continue..."
Read-Host
$Credential = Get-Credential -Message "Credential for access to all remote computers. Usually the domain user account in Administrator group."
}
foreach ($Computer in Get-Content $args[0]) {
Write-Output ""
Write-Output "Running the script against $computer computer..."
Write-Output ""
if ($RequireCredential) {
$Session = New-PSSession $Computer -Credential $Credential
} else {
$Session = New-PSSession $Computer
}
try {
Invoke-Command -Session $Session -FilePath $ScriptPath
Copy-Item $OutputPath -Destination C:\ -Recurse -Force -FromSession $Session
Disconnect-PSSession -Session $Session
Remove-PSSession -Session $Session
} catch {
Write-Host $_.ScriptStackTrace
}
}
if (Get-ChildItem $OutputPath) {
Get-ChildItem $OutputPath\*.csv |
ForEach-Object { Import-Csv $_ } |
Export-Csv $OutputPath\iis-info-all.csv -NoTypeInformation
}
}
Write-Output ""
Write-Output "The script was run successfully."