update nextcloud, update tools
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
## Stop a process
|
||||
Function End-Process
|
||||
{
|
||||
param(
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Name,
|
||||
[Parameter(Mandatory=$false)][switch]$Match=$false,
|
||||
[Parameter(Mandatory=$false)][switch]$Force
|
||||
)
|
||||
ForEach ( $item in $Name ) {
|
||||
$process = Get-Process -Name $item -ErrorAction SilentlyContinue
|
||||
If ( !($process) ) {
|
||||
LogMsg "Cannot find the process '${item}'"
|
||||
If ( $Match ) {
|
||||
LogMsg "Attempting to match '${item}' to all running process names"
|
||||
ForEach ( $proc in ((Get-Process | Where { $_.Name -match $item }).Name) ) {
|
||||
If ( $Force ) { End-Process -Name $proc -Force } Else { End-Process -Name $proc }
|
||||
}
|
||||
}
|
||||
}
|
||||
Else {
|
||||
$name = $process.Name
|
||||
If ( $Force ) {
|
||||
LogMsg "Forcefully stopping '${name}' process"
|
||||
Stop-Process $process -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
Else {
|
||||
LogMsg "Attempting to gracefully close '${name}' process"
|
||||
$process.CloseMainWindow() | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
## Define URL for downloading the installer
|
||||
$URL = 'https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/resources/Nextcloud-3.0.1-setup.exe'
|
||||
$URL = 'https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/resources/Nextcloud-3.0.3-setup.exe'
|
||||
$FILE = '{0}{1}' -f (GetTempPath), (Split-Path $URL -Leaf)
|
||||
|
||||
$FILE = Download-File -URL $URL -File $FILE
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
Function Remediation-RestartService
|
||||
{
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Name)
|
||||
ForEach ( $service in $Name )
|
||||
{
|
||||
Remediation-StopService -Name $service
|
||||
}
|
||||
}
|
||||
|
||||
Function Remediation-StopService
|
||||
{
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Name)
|
||||
ForEach ( $service in $Name )
|
||||
{
|
||||
$status = (Get-Service -Name $service -ErrorAction SilentlyContinue).Status
|
||||
If ( $status -ne "Stopped" )
|
||||
{
|
||||
LogMsg " Stopping '${service}'"
|
||||
Try { Stop-Service -Name $service -Force -ErrorAction SilentlyContinue }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
}
|
||||
Else { LogMsg " '${service}' service is already stopped" }
|
||||
}
|
||||
}
|
||||
|
||||
Function Remediation-StartService
|
||||
{
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Name)
|
||||
ForEach ( $service in $Name )
|
||||
{
|
||||
$service = "bogus"
|
||||
$status = (Get-Service -Name $service -ErrorAction SilentlyContinue).Status
|
||||
If ( !($status) )
|
||||
{
|
||||
Write-Host "Cannot find the service '${service}', attempting to match a service display name instead"
|
||||
$status = Get-Service | Where { $_.DisplayName -match $service }
|
||||
Write-Host $status
|
||||
}
|
||||
Else { Write-Host "Status is: ${status}" }
|
||||
If ( $status -eq "StartPending" )
|
||||
{ LogMsg " '${service}' was previously started, and is still starting up" }
|
||||
ElseIf ( $status -ne "Running" )
|
||||
{
|
||||
LogMsg " Starting '${service}'"
|
||||
Try { Start-Service -Name $service -Force -ErrorAction SilentlyContinue }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
}
|
||||
Else { LogMsg " '${service}' service is already running" }
|
||||
}
|
||||
}
|
||||
|
||||
[string[]]$list = "apple", "orange", "banana", "pineapple"
|
||||
Write-Host "Here are the list items: ${list}"
|
||||
@@ -2,71 +2,48 @@
|
||||
$MSPName = "Emberkom"
|
||||
|
||||
## Setup the MSP management directories
|
||||
If ( Test-Path ${Env:ProgramData} )
|
||||
{ $MSPRoot = "${Env:ProgramData}\${MSPName}" } Else { $MSPRoot = "${Env:SystemDrive}\${MSPName}" }
|
||||
$MSPScripts = "${MSPRoot}\Scripts"
|
||||
$MSPTools = "${MSPRoot}\Tools"
|
||||
$MSPLogs = "${MSPRoot}\Logs"
|
||||
If ( Test-Path ${Env:ProgramData} ) {$RootDirectory = "${Env:ProgramData}\${MSPName}" } Else { $RootDirectory = "${Env:SystemDrive}\${MSPName}" }
|
||||
$ScriptsDirectory = "${RootDirectory}\Scripts"
|
||||
$ToolsDirectory = "${RootDirectory}\Tools"
|
||||
$LogsDirectory = "${RootDirectory}\Logs"
|
||||
|
||||
## Make sure all the management directories exist
|
||||
$MSPRoot, $MSPScripts, $MSPTools, $MSPLogs | ForEach-Object {
|
||||
If ( !(Test-Path $_) )
|
||||
{
|
||||
$RootDirectory, $ScriptsDirectory, $ToolsDirectory, $LogsDirectory | ForEach-Object {
|
||||
If ( !(Test-Path $_) ) {
|
||||
Try { New-Item -Path $_ -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null }
|
||||
Catch { Write-Output $_.Exception.Message }
|
||||
}
|
||||
}
|
||||
|
||||
## Setup the log file for messages generated when this script is run
|
||||
$LogFile = '{0}\ManagementScript_{1}.log' -f $MSPLogs, (Get-Date -Format "yyyyMMddHHmmss")
|
||||
$LogFile = '{0}\ManagementScript_{1}.log' -f $LogsDirectory, (Get-Date -Format "yyyyMMddHHmmssffff")
|
||||
If ( !(Test-Path $LogFile) ) { New-Item -Path $LogFile -ItemType File -Force | Out-Null }
|
||||
|
||||
## Function to log activity to the configured log directory
|
||||
Function Log
|
||||
{
|
||||
param(
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message,
|
||||
[Parameter(Mandatory=$false)][switch]$Error=$false,
|
||||
[Parameter(Mandatory=$false)][string]$Name
|
||||
)
|
||||
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
If ( $Error ) { $LogEntry = "${Timestamp} - Error: ${Message}" }
|
||||
Else { $LogEntry = "${Timestamp} - ${Message}" }
|
||||
Add-Content -Path $LogFile -Value $LogEntry
|
||||
Write-Output $LogEntry
|
||||
}
|
||||
|
||||
## Log a message to the log file
|
||||
Function LogMsg
|
||||
{
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message)
|
||||
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
$LogEntry = "${Timestamp} - ${Message}"
|
||||
Add-Content -Path $LogFile -Value $LogEntry
|
||||
#Write-Output $LogEntry
|
||||
Function LogMsg {
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message)
|
||||
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
$LogEntry = "${Timestamp} - ${Message}"
|
||||
Add-Content -Path $LogFile -Value $LogEntry
|
||||
}
|
||||
|
||||
## Log an error to the log file
|
||||
Function LogErr
|
||||
{
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message)
|
||||
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
$LogEntry = "${Timestamp} - Error: ${Message}"
|
||||
Add-Content -Path $LogFile -Value $LogEntry
|
||||
#Write-Output "Error: ${LogEntry}"
|
||||
Function LogErr {
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message)
|
||||
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
$LogEntry = "${Timestamp} - Error: ${Message}"
|
||||
Add-Content -Path $LogFile -Value $LogEntry
|
||||
}
|
||||
|
||||
## Function to determine if the current user context is an Administrator
|
||||
Function IsAdmin
|
||||
{
|
||||
If ( ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) )
|
||||
{ Return $true } Else { Return $false }
|
||||
Function IsAdmin {
|
||||
If ( ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) )
|
||||
{ Return $true } Else { Return $false }
|
||||
}
|
||||
|
||||
## Run a script from the live repo or from a local path
|
||||
Function Run-Script
|
||||
{
|
||||
param(
|
||||
Function Run-Script {
|
||||
param(
|
||||
## Parameters for live-ps scripts
|
||||
[Parameter(Mandatory=$false,ParameterSetName='live-ps')]
|
||||
[string]$Type='management-scripts',
|
||||
@@ -90,27 +67,22 @@ param(
|
||||
[Parameter(Mandatory=$false,ParameterSetName='live-ps')]
|
||||
[Parameter(Mandatory=$false,ParameterSetName='local')]
|
||||
[switch]$ForceClose=$false
|
||||
)
|
||||
If ( $Arguments -eq "null" ) { $Arguments = $null }
|
||||
If ( $HaltIfRunning -eq "null" ) { $HaltIfRunning = $null }
|
||||
If ( $LivePSScript )
|
||||
{
|
||||
)
|
||||
If ( $Arguments -eq "null" ) { $Arguments = $null }
|
||||
If ( $HaltIfRunning -eq "null" ) { $HaltIfRunning = $null }
|
||||
If ( $LivePSScript ) {
|
||||
$URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${LivePSScript}.ps1"
|
||||
If ( !(IsURLValid -URL $URL) ) { LogMsg "The specified script does not exist: ${LivePSScript}" ; Exit }
|
||||
}
|
||||
If ( $Path ) { If ( !(Test-Path $Path) ) { LogMsg "The specified script does not exist: ""${Path}""" ; Exit } }
|
||||
If ( ($HaltIfRunning -ne $null ) -and ($ForceClose) -and ( !(IsAdmin) ) ) { LogMsg "Cannot close dependent apps because this task does not have administrative privileges" ; Exit }
|
||||
If ( $HaltIfRunning )
|
||||
{
|
||||
}
|
||||
If ( $Path ) { If ( !(Test-Path $Path) ) { LogMsg "The specified script does not exist: ""${Path}""" ; Exit } }
|
||||
If ( ($HaltIfRunning -ne $null ) -and ($ForceClose) -and ( !(IsAdmin) ) ) { LogMsg "Cannot close dependent apps because this task does not have administrative privileges" ; Exit }
|
||||
If ( $HaltIfRunning ) {
|
||||
$Halt = $false
|
||||
LogMsg "Checking for running instances of the following dependent apps:"
|
||||
ForEach ( $name in $HaltIfRunning)
|
||||
{
|
||||
ForEach ( $name in $HaltIfRunning) {
|
||||
$RunningInstances = Get-Process | Where { $_.Name -like "${name}" }
|
||||
If ( $RunningInstances )
|
||||
{
|
||||
If ( $ForceClose -eq $true )
|
||||
{
|
||||
If ( $RunningInstances ) {
|
||||
If ( $ForceClose -eq $true ) {
|
||||
Try { $name | Stop-Process -Force }
|
||||
Catch { LogErr $_.Exception.Message ; Exit }
|
||||
Log " ""${name}"" (force closed)" -Name $LogName
|
||||
@@ -120,11 +92,9 @@ If ( $HaltIfRunning )
|
||||
Else { LogMsg " ""${name}"" (not running)" }
|
||||
}
|
||||
If ( $Halt -eq $true ) { LogMsg "Dependent apps are currently in use and are preventing the specified script from running" ; Exit }
|
||||
}
|
||||
switch ($PSCmdlet.ParameterSetName)
|
||||
{
|
||||
'live-ps'
|
||||
{
|
||||
}
|
||||
switch ($PSCmdlet.ParameterSetName) {
|
||||
'live-ps' {
|
||||
LogMsg "Retrieving : ${LivePSScript}.ps1"
|
||||
Try { $Script = (New-Object Net.WebClient).DownloadString($URL) }
|
||||
Catch { LogErr $_.Exception.Message ; Exit }
|
||||
@@ -135,68 +105,59 @@ switch ($PSCmdlet.ParameterSetName)
|
||||
Catch { LogErr $_.Exception.Message ; Exit }
|
||||
}
|
||||
|
||||
'local'
|
||||
{
|
||||
'local' {
|
||||
LogMsg "Executing: ${Path} ${Arguments}"
|
||||
If ( $NoWait )
|
||||
{
|
||||
If ( $NoWait ) {
|
||||
Try { Start-Process "${Path}" -ArgumentList "{$Arguments}" }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
}
|
||||
Else
|
||||
{
|
||||
Else {
|
||||
LogMsg " Waiting for script to finish..."
|
||||
Try { Start-Process "${Path}" -ArgumentList "{$Arguments}" -Wait }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
LogMsg " Finished"
|
||||
}
|
||||
}
|
||||
}
|
||||
## Write the log file output to the console
|
||||
If ( Test-Path $LogFile ) { Get-Content -Path $LogFile | Write-Output }
|
||||
Else { Write-Output "Log file not found at ""$LogFile""" }
|
||||
}
|
||||
## Write the log file output to the console
|
||||
If ( Test-Path $LogFile ) { Get-Content -Path $LogFile | Write-Output }
|
||||
Else { Write-Output "Log file not found at ""$LogFile""" }
|
||||
}
|
||||
|
||||
## Return a Powershell version object from a string
|
||||
Function Get-Version
|
||||
{
|
||||
param([Parameter(Mandatory=$true)][string]$Version)
|
||||
[int]$Sets = 4
|
||||
$VersionArray = $Version.Split('.')
|
||||
If ( $VersionArray.Count -le $Sets ) { $Sets = $VersionArray.Count }
|
||||
For ($i = 0; $i -le ($Sets - 1); $i++)
|
||||
{ $Return = '{0}.{1}' -f $Return, $VersionArray[$i] }
|
||||
Return [version]$Return.Trim('.')
|
||||
Function Get-Version {
|
||||
param([Parameter(Mandatory=$true)][string]$Version)
|
||||
[int]$Sets = 4
|
||||
$VersionArray = $Version.Split('.')
|
||||
If ( $VersionArray.Count -le $Sets ) { $Sets = $VersionArray.Count }
|
||||
For ($i = 0; $i -le ($Sets - 1); $i++) {
|
||||
$Return = '{0}.{1}' -f $Return, $VersionArray[$i] }
|
||||
Return [version]$Return.Trim('.')
|
||||
}
|
||||
|
||||
## Determines whether a URL is valid
|
||||
Function IsURLValid
|
||||
{
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$URL)
|
||||
Try
|
||||
{
|
||||
Function IsURLValid {
|
||||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$URL)
|
||||
Try {
|
||||
$HTTPRequest = [System.Net.WebRequest]::Create($URL)
|
||||
$HTTPResponse = $HTTPRequest.GetResponse()
|
||||
$HTTPStatus = [int]$HTTPResponse.StatusCode
|
||||
$HTTPResponse.Close()
|
||||
}
|
||||
Catch {}
|
||||
If ($HTTPStatus -eq 200) { Return $true }
|
||||
Else { Return $false }
|
||||
}
|
||||
Catch { }
|
||||
If ($HTTPStatus -eq 200) { Return $true }
|
||||
Else { Return $false }
|
||||
}
|
||||
|
||||
## Downloads a file from a URL
|
||||
Function Download-File
|
||||
{
|
||||
param(
|
||||
Function Download-File {
|
||||
param(
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$URL,
|
||||
[Parameter(Mandatory=$false,ValueFromPipeline=$false)][string]$File
|
||||
)
|
||||
If ( !($File) ) { $File = '{0}{1}' -f (GetTempPath), (Split-Path $URL -Leaf) }
|
||||
If ( IsURLValid $URL )
|
||||
{
|
||||
If ( Test-Path $File )
|
||||
{
|
||||
)
|
||||
If ( !($File) ) { $File = '{0}{1}' -f (GetTempPath), (Split-Path $URL -Leaf) }
|
||||
If ( IsURLValid $URL ) {
|
||||
If ( Test-Path $File ) {
|
||||
LogMsg "Deleting existing file: ""${File}"""
|
||||
Try { Remove-Item $File -Force }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
@@ -206,123 +167,229 @@ If ( IsURLValid $URL )
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
If ( Test-Path "${File}" ) { Return "${File}" }
|
||||
Else { LogMsg "Downloaded file does not exist at ""${File}""" ; Return $false }
|
||||
}
|
||||
Else { LogMsg "URL is not valid or file cannot be reached: ${URL}" }
|
||||
}
|
||||
Else { LogMsg "URL is not valid or file cannot be reached: ${URL}" }
|
||||
}
|
||||
|
||||
## Install a program from a provided path
|
||||
Function Install-Program
|
||||
{
|
||||
param(
|
||||
[Parameter(Mandatory=$true)][string]$Path,
|
||||
Function Install-Program {
|
||||
param(
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Path,
|
||||
[Parameter(Mandatory=$false)][string]$Arguments = '',
|
||||
[Parameter(Mandatory=$false)][switch]$Delete = $false
|
||||
)
|
||||
If ( Test-Path "${Path}" )
|
||||
{
|
||||
)
|
||||
If ( Test-Path "${Path}" ) {
|
||||
LogMsg "Running installer: ""${Path}"" ${Arguments}"
|
||||
Try { Start-Process "${Path}" -ArgumentList "${Arguments}" -Wait }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
LogMsg "Installer finished"
|
||||
If ( $Delete )
|
||||
{
|
||||
If ( $Delete ) {
|
||||
LogMsg "Deleting installer: ""${Path}"""
|
||||
Try { Remove-Item -Path "${Path}" -Force }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
}
|
||||
}
|
||||
Else { LogMsg "The specified file could not be found: ""${Path}""" }
|
||||
}
|
||||
Else { LogMsg "The specified file could not be found: ""${Path}""" }
|
||||
}
|
||||
|
||||
## Function to compare current Windows version with a supplied version
|
||||
## Compare current Windows version with a supplied version
|
||||
## The value supplied must be a string and must include at least 1 decimal
|
||||
Function IsWindowsVersion
|
||||
{
|
||||
param(
|
||||
Function IsWindowsVersion {
|
||||
param(
|
||||
[Parameter(Mandatory=$true,ParameterSetName='gt')][string]$gt,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='ge')][string]$ge,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='lt')][string]$lt,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='le')][string]$le,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='eq')][string]$eq
|
||||
)
|
||||
[version]$WindowsVersion = [System.Environment]::OSVersion.Version
|
||||
switch ($PSCmdlet.ParameterSetName)
|
||||
{
|
||||
)
|
||||
[version]$WindowsVersion = [System.Environment]::OSVersion.Version
|
||||
switch ($PSCmdlet.ParameterSetName) {
|
||||
'gt' { If ( $WindowsVersion -gt (Get-Version $gt) ) { Return $true } Else { Return $false } }
|
||||
'ge' { If ( $WindowsVersion -ge (Get-Version $ge) ) { Return $true } Else { Return $false } }
|
||||
'lt' { If ( $WindowsVersion -lt (Get-Version $lt) ) { Return $true } Else { Return $false } }
|
||||
'le' { If ( $WindowsVersion -le (Get-Version $le) ) { Return $true } Else { Return $false } }
|
||||
'eq' { If ( $WindowsVersion -eq (Get-Version $eq) ) { Return $true } Else { Return $false } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
## Compare current Windows build with a supplied version
|
||||
## The value supplied must be an int
|
||||
Function IsWindowsBuild
|
||||
{
|
||||
param(
|
||||
Function IsWindowsBuild {
|
||||
param(
|
||||
[Parameter(Mandatory=$true,ParameterSetName='gt')][int]$gt,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='ge')][int]$ge,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='lt')][int]$lt,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='le')][int]$le,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='eq')][int]$eq
|
||||
)
|
||||
[int]$WindowsBuild = [System.Environment]::OSVersion.Version.Build
|
||||
switch ($PSCmdlet.ParameterSetName)
|
||||
{
|
||||
)
|
||||
[int]$WindowsBuild = [System.Environment]::OSVersion.Version.Build
|
||||
switch ($PSCmdlet.ParameterSetName) {
|
||||
'gt' { If ( $WindowsBuild -gt $gt ) { Return $true } Else { Return $false } }
|
||||
'ge' { If ( $WindowsBuild -ge $ge ) { Return $true } Else { Return $false } }
|
||||
'lt' { If ( $WindowsBuild -lt $lt ) { Return $true } Else { Return $false } }
|
||||
'le' { If ( $WindowsBuild -le $le ) { Return $true } Else { Return $false } }
|
||||
'eq' { If ( $WindowsBuild -eq $eq ) { Return $true } Else { Return $false } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
## Get the path to the TEMP folder (context dependant)
|
||||
Function GetTempPath
|
||||
{ Return [System.IO.Path]::GetTempPath() }
|
||||
Function GetTempPath { Return [System.IO.Path]::GetTempPath() }
|
||||
|
||||
## Determine if the system is 64-bit or not
|
||||
Function Is64bit
|
||||
{
|
||||
switch ( $(Get-CimInstance -ClassName Win32_OperatingSystem -Property OSArchitecture | Select-Object -ExpandProperty OSArchitecture) )
|
||||
{ '64-bit' { Return $true } '32-bit' { Return $false } }
|
||||
Function Is64bit {
|
||||
switch ( $(Get-CimInstance -ClassName Win32_OperatingSystem -Property OSArchitecture | Select-Object -ExpandProperty OSArchitecture) )
|
||||
{ '64-bit' { Return $true } '32-bit' { Return $false } }
|
||||
}
|
||||
|
||||
## Determine if the Powershell process is 64-bit or not
|
||||
Function Is64bitShell
|
||||
{ If ( [System.Environment]::Is64BitProcess ) { Return $true } Else { Return $false } }
|
||||
Function Is64bitShell { If ( [System.Environment]::Is64BitProcess ) { Return $true } Else { Return $false } }
|
||||
|
||||
## Determine if a user is in a built-in role or specified group
|
||||
Function IsMemberOf {
|
||||
param(
|
||||
param(
|
||||
[Parameter(Mandatory=$false)]
|
||||
[switch]$Builtin=$false,
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
|
||||
[string]$Group
|
||||
)
|
||||
If ( $Builtin ) {
|
||||
)
|
||||
If ( $Builtin ) {
|
||||
Switch ( $Group ) {
|
||||
("Administrator" -or "Administrators")
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::Administrator ; break }
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::Administrator ; Break }
|
||||
("Backup Operator" -or "Backup Operators" -or "BackupOperator" -or "BackupOperators")
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::BackupOperator ; break }
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::BackupOperator ; Break }
|
||||
("System Operator" -or "System Operators" -or "SystemOperator" -or "SystemOperators")
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::SystemOperator ; break }
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::SystemOperator ; Break }
|
||||
("Account Operator" -or "Account Operators" -or "AccountOperator" -or "AccountOperators")
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::AccountOperator ; break }
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::AccountOperator ; Break }
|
||||
("Print Operator" -or "Print Operators" -or "PrintOperator" -or "PrintOperators")
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::PrintOperator ; break }
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::PrintOperator ; Break }
|
||||
("Replicator" -or "Replicators")
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::Replicator ; break }
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::Replicator ; Break }
|
||||
("Power User" -or "Power Users" -or "PowerUser" -or "PowerUsers")
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::PowerUser ; break }
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::PowerUser ; Break }
|
||||
("User" -or "Users")
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::User ; break }
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::User ; Break }
|
||||
("Guest" -or "Guests")
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::Guest ; break }
|
||||
default { ; break }
|
||||
{ $Group = [System.Security.Principal.WindowsBuiltInRole]::Guest ; Break }
|
||||
default { ; Break }
|
||||
}
|
||||
}
|
||||
Return ([Security.Principal.WindowsPrincipal][System.Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole($Group)
|
||||
}
|
||||
|
||||
## Start, stop, or restart a list of services
|
||||
Function Control-Service
|
||||
{
|
||||
param(
|
||||
[Parameter(Mandatory=$true,ParameterSetName='start')]
|
||||
[switch]$Start,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='stop')]
|
||||
[switch]$Stop,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='restart')]
|
||||
[switch]$Restart,
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName='start')]
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName='stop')]
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName='restart')]
|
||||
[string[]]$Name,
|
||||
[Parameter(Mandatory=$false,ParameterSetName='start')]
|
||||
[Parameter(Mandatory=$false,ParameterSetName='stop')]
|
||||
[Parameter(Mandatory=$false,ParameterSetName='restart')]
|
||||
[switch]$Match=$false
|
||||
)
|
||||
switch ($PSCmdlet.ParameterSetName) {
|
||||
'start' {
|
||||
$statuscondition = 'Running'
|
||||
$verb = 'start'
|
||||
$verbing = 'Starting'
|
||||
$verbed = $statuscondition.ToLower()
|
||||
}
|
||||
'stop' {
|
||||
$statuscondition = 'Stopped'
|
||||
$verb = 'stop'
|
||||
$verbing = 'Stopping'
|
||||
$verbed = $statuscondition.ToLower()
|
||||
}
|
||||
'restart' {
|
||||
$LaunchCommand = $MyInvocation.MyCommand
|
||||
$ArgString = $MyInvocation.Line
|
||||
$StopCmd = $ArgString -replace ' -Restart ', ' -Stop '
|
||||
$StartCmd = $ArgString -replace ' -Restart ', ' -Start '
|
||||
Invoke-Expression $StopCmd
|
||||
Invoke-Expression $StartCmd
|
||||
Exit
|
||||
}
|
||||
}
|
||||
ForEach ( $item in $Name ) {
|
||||
$service = Get-Service -Name $item -ErrorAction SilentlyContinue
|
||||
If ( !($service) ) {
|
||||
LogMsg "Cannot find the service '${item}'"
|
||||
If ( $Match ) {
|
||||
LogMsg "Attempting to match '${item}' to service display names"
|
||||
ForEach ( $svc in ((Get-Service | Where { $_.DisplayName -match $item }).Name) ) {
|
||||
Switch ( $verb) {
|
||||
'start' { Control-Service -Start -Name $svc }
|
||||
'stop' { Control-Service -Stop -Name $svc }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Else {
|
||||
$name = $service.Name
|
||||
$fullname = $service.DisplayName
|
||||
If ( ($fullname -eq $null) -or ($fullname -eq '') -or ($fullname -eq $name) ) {
|
||||
$displayname = "'${name}'"
|
||||
}
|
||||
Else {
|
||||
$displayname = """${fullname}"" (${name})"
|
||||
}
|
||||
If ( $service.Status -ne $statuscondition ) {
|
||||
LogMsg " ${verbing} ${displayname}"
|
||||
Switch ( $verb) {
|
||||
'start' {
|
||||
Try { Start-Service -Name $svc }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
}
|
||||
'stop' {
|
||||
Try { Stop-Service -Name $svc -Force }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
}
|
||||
}
|
||||
}
|
||||
Else { LogMsg " The ${displayname} service is already ${verbed}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
Return ([Security.Principal.WindowsPrincipal][System.Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole($Group)
|
||||
|
||||
## Stop a process
|
||||
Function End-Process
|
||||
{
|
||||
param(
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Name,
|
||||
[Parameter(Mandatory=$false)][switch]$Match=$false,
|
||||
[Parameter(Mandatory=$false)][switch]$Force
|
||||
)
|
||||
ForEach ( $item in $Name ) {
|
||||
$process = Get-Process -Name $item -ErrorAction SilentlyContinue
|
||||
If ( !($process) ) {
|
||||
LogMsg "Cannot find the process '${item}'"
|
||||
If ( $Match ) {
|
||||
LogMsg "Attempting to match '${item}' to all running process names"
|
||||
ForEach ( $proc in ((Get-Process | Where { $_.Name -match $item }).Name) ) {
|
||||
If ( $Force ) { End-Process -Name $proc -Force } Else { End-Process -Name $proc }
|
||||
}
|
||||
}
|
||||
}
|
||||
Else {
|
||||
$name = $process.Name
|
||||
If ( $Force ) {
|
||||
LogMsg "Forcefully stopping '${name}' process"
|
||||
Stop-Process $process -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
Else {
|
||||
LogMsg "Attempting to gracefully close '${name}' process"
|
||||
$process.CloseMainWindow() | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user