Skip to content

Commit 151101d

Browse files
authored
New script 🍍
1 parent 61588bd commit 151101d

1 file changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Author: Blake Drumm (blakedrumm@microsoft.com)
2+
# Date created: February 29th, 2024
3+
# Description: This script will allow you to make the System Center Operations Manager PowerShell module portable. It will zip up the output folder and all you have to do is run the Install-SCOMModule.ps1 file
4+
# on the remote machine where you want to install the SCOM PowerShell module. (the Install-SCOMModule.ps1 file is located in the output folder / zip.)
5+
6+
#---------------------------------------------
7+
# Variables to edit
8+
# Define the folder path
9+
$folderPath = "C:\Temp\SCOM-PowerShellModule"
10+
# Define the zip file path for output
11+
$zipFilePath = "C:\Temp\SCOM-PowerShellModule.zip"
12+
#---------------------------------------------
13+
14+
# The path to the Powershell folder in the SCOM installation folder
15+
$powerShellFolder = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2" -Name InstallDirectory
16+
# Uncomment the below to force the path to a specific directory, instead of detecting automatically from the registry.
17+
#$powerShellFolder = "C:\Program Files\Microsoft System Center\Operations Manager\Powershell"
18+
Write-Output "PowerShell folder path: $powerShellFolder"
19+
20+
# Check if the folder exists
21+
if (-Not (Test-Path -Path $folderPath))
22+
{
23+
# Folder does not exist, so create it
24+
New-Item -ItemType Directory -Path $folderPath -Force | Out-Null
25+
Write-Output "Output folder '$folderPath' has been created."
26+
}
27+
else
28+
{
29+
# Folder already exists
30+
Write-Output "Output folder '$folderPath' already exists."
31+
}
32+
33+
Get-ChildItem 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.EnterpriseManagement*' | ForEach-Object {
34+
Copy-Item -Path $_.FullName -Destination $folderPath -Force -Recurse
35+
}
36+
37+
Write-Output " - Copied the required GAC_MSIL files."
38+
39+
try
40+
{
41+
$resolvedPath = (Resolve-Path -Path $powerShellFolder -ErrorAction Stop).Path
42+
Copy-Item -Path $resolvedPath -Destination $folderPath -Recurse -Force
43+
}
44+
catch
45+
{
46+
Write-Warning "Unable to locate the path '$powerShellFolder'!"
47+
break
48+
}
49+
50+
# Create a text file with the $powerShellFolder path inside the $folderPath
51+
Set-Content -Path "$folderPath\PowerShellFolderInfo.txt" -Value "$powerShellFolder"
52+
53+
Set-Content -Path "$folderPath\Install-SCOMModule.ps1" -Value @"
54+
[CmdletBinding()]
55+
param ()
56+
57+
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
58+
{
59+
Write-Warning "This script must be run as an administrator!"
60+
return
61+
}
62+
63+
# Define the registry path where .NET Framework versions are stored
64+
`$regPath = "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"
65+
66+
# Check if the registry path exists
67+
if (Test-Path `$regPath)
68+
{
69+
# Get the version value from the registry
70+
`$versionValue = Get-ItemProperty -Path `$regPath -Name "Release" | Select-Object -ExpandProperty "Release"
71+
72+
# Check the version value for .NET Framework 4.7.2 or 4.8
73+
# .NET Framework 4.7.2 release key: 461808
74+
# .NET Framework 4.8 release key: 528040
75+
if (`$versionValue -ge 461808 -and `$versionValue -lt 528040)
76+
{
77+
Write-Output " - .NET Framework 4.7.2 is installed."
78+
}
79+
elseif (`$versionValue -eq 528040)
80+
{
81+
Write-Output " - .NET Framework 4.8 is installed."
82+
}
83+
elseif (`$versionValue -gt 528040)
84+
{
85+
Write-Output " - Higher than .NET Framework 4.8 is installed. Unable to proceed."
86+
return
87+
}
88+
else
89+
{
90+
Write-Warning "Neither .NET Framework 4.7.2 nor 4.8 is installed. Unable to proceed."
91+
return
92+
}
93+
}
94+
else
95+
{
96+
Write-Warning "Unable to find .NET Framework 4.x installation. Unable to proceed."
97+
return
98+
}
99+
100+
101+
`$powerShellFolder = Get-Content .\PowerShellFolderInfo.txt
102+
try
103+
{
104+
`$gacPath = (Resolve-Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL" -ErrorAction Stop).Path
105+
}
106+
catch
107+
{
108+
Write-Warning "Unable to locate the path: 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL'. Unable to proceed."
109+
return
110+
}
111+
Get-ChildItem ".\Microsoft.EnterpriseManagement*" | % {
112+
`$resolvePath = Resolve-Path `$gacPath\`$(`$_.Name)
113+
if (-NOT `$resolvePath)
114+
{
115+
Copy-Item -Path `$_.FullName -Destination `$gacPath -Force -Recurse
116+
}
117+
}
118+
Copy-Item -Path .\Powershell\ -Destination `$powerShellFolder -Force -Recurse
119+
120+
`$p = [Environment]::GetEnvironmentVariable("PSModulePath")
121+
if (-NOT `$p -contains `$powerShellFolder)
122+
{
123+
`$p += ";`$powerShellFolder"
124+
[Environment]::SetEnvironmentVariable("PSModulePath", `$p, 'Machine')
125+
Write-Output " - Added module to the machine level environmental variable (PSModulePath)"
126+
}
127+
else
128+
{
129+
Write-Output " - Module is already in the machine level environmental variable (PSModulePath)"
130+
}
131+
try
132+
{
133+
Import-Module OperationsManager -Verbose -ErrorAction Stop
134+
}
135+
catch
136+
{
137+
Write-Warning "Unable to import the SCOM PowerShell Module!`n`$_"
138+
return
139+
}
140+
Write-Output "Completed importing the SCOM PowerShell Module!
141+
"@
142+
143+
Set-Content -Path "$folderPath\Readme.txt" -Value @"
144+
In order to install the SCOM PowerShell Module on a machine, run the PowerShell script 'Install-SCOMModule.ps1' as Administrator.
145+
"@
146+
147+
# Zip the folder, including the text file
148+
Compress-Archive -Path "$folderPath\*" -DestinationPath $zipFilePath -Force
149+
150+
Write-Output "Output File: '$zipFilePath'"

0 commit comments

Comments
 (0)