Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Author : TDiff
- # Purpose : Simplified WMI Commands to manage distant computers
- # Requirements : Launch powershell with credentials having WMI privileges
- ####################################################
- ###################### Infos ######################
- ####################################################
- echo "DistantTools loaded.`n"
- echo "Commands :`n"
- echo "* Distant Powershell :";
- echo " - Execute-DistantCommand ComputerName `"Commands`"`n";
- echo "* Distant Services Managment :";
- echo " - Get-DistantService ComputerName `"ServiceNa*`"";
- echo " - Start-DistantService ComputerName `"ServiceNa*`"";
- echo " - Stop-DistantService ComputerName `"ServiceNa*`"`n";
- echo "* Distant Processes Managment :";
- echo " - Get-DistantProcess ComputerName `"ProcessNa*`"";
- echo " - Start-DistantProcess ComputerName `"ProcessNa*`"";
- echo " - Stop-DistantProcess ComputerName `"ProcessNa*`"`n";
- echo "* System Informations :";
- echo " - Get-DistantSystemInfos ComputerName";
- echo " - Get-DistantCompressedFiles ComputerName`n";
- ###################################################################
- ###################### Powershell Execution ######################
- ###################################################################
- # Remove comments and linefeeds of a script and return a string object
- function Remove-Comments {
- [CmdletBinding(DefaultParameterSetName='FilePath' )]
- Param (
- [Parameter(Position=0,Mandatory=$True,ParameterSetName='FilePath' )] [ValidateNotNullOrEmpty()] [String]$Path,
- [Parameter(Position=0,ValueFromPipeline=$True,Mandatory=$True,ParameterSetName='ScriptBlock')] [ValidateNotNullOrEmpty()]
- [ScriptBlock]$ScriptBlock
- )
- Set-StrictMode -Version 2;
- if ($PSBoundParameters['Path']) {
- gci $Path -ErrorAction Stop | Out-Null;
- $ScriptBlockString=[IO.File]::ReadAllText((Resolve-Path $Path));
- $ScriptBlock=[ScriptBlock]::Create($ScriptBlockString);
- } Else { $ScriptBlockString=$ScriptBlock.ToString() }
- $Tokens=[System.Management.Automation.PSParser]::Tokenize($ScriptBlock,[Ref]$Null) | Where{ $_.Type-ne'Comment' };
- $StringBuilder=New-Object Text.StringBuilder;
- $CurrentColumn=1;
- $NewlineCount=0;
- foreach($CurrentToken in $Tokens) {
- if (($CurrentToken.Type -eq 'NewLine') -or ($CurrentToken.Type -eq 'LineContinuation')){
- $CurrentColumn=1;
- if ($NewlineCount -eq 0) { $StringBuilder.AppendLine()|Out-Null }
- $NewlineCount++
- } else {
- $NewlineCount=0;
- if (($CurrentColumn -lt $CurrentToken.StartColumn) -and ($CurrentColumn -ne 1)) { $StringBuilder.Append(' ') | Out-Null }
- $CurrentTokenEnd = $CurrentToken.Start + $CurrentToken.Length-1;
- if (($CurrentToken.Type -eq 'String') -and ($CurrentToken.EndLine -gt $CurrentToken.StartLine)) {
- $LineCounter = $CurrentToken.StartLine;
- $StringLines = $( -join $ScriptBlockString[$CurrentToken.Start..$CurrentTokenEnd] -split '`r`n');
- foreach ($StringLine in $StringLines) {
- $StringBuilder.Append($StringLine) | Out-Null;
- $LineCounter++;
- }
- } else { $StringBuilder.Append(( -join $ScriptBlockString[$CurrentToken.Start..$CurrentTokenEnd])) | Out-Null }
- $CurrentColumn = $CurrentToken.EndColumn
- }
- }
- Write-Output([ScriptBlock]::Create($StringBuilder.ToString()))
- }
- # Execute powershell command(s) or script (use Remove-Comments) on a distant computer
- function Execute-DistantCommand ([string] $ComputerName, [string] $Cmd) {
- $ComputerName = [System.Net.Dns]::GetHostAddresses("$ComputerName")[0];
- $sEncodedCommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("&{" + $Cmd + "}"));
- $oResult = iwmi -ComputerName $ComputerName -EnableAllPrivileges -Path win32_process -Name create -ArgumentList "powershell -NoE -enc $sEncodedCommand";
- if ($oResult.ProcessId -gt 0) { return $true; } return $false;
- }
- ################################################################
- ###################### Service Managment ######################
- ################################################################
- # Get service(s) from a distant computer
- function Get-DistantService ([string] $ComputerName, [string] $ServiceName) {
- $ServiceName = $ServiceName -replace '\*','%';
- $ComputerName = [System.Net.Dns]::GetHostAddresses("$ComputerName")[0];
- return (gwmi -ComputerName $ComputerName -Query "SELECT * FROM Win32_Service WHERE Name LIKE '$ServiceName'");
- }
- # Start a distant service
- function Start-DistantService ([string] $ComputerName, [string] $ServiceName) {
- $aServices = Get-DistantService $ComputerName $ServiceName;
- if ($aServices -ne $null) {
- foreach ($oService in $aServices) {
- if (!$oService.Started) {
- return $oService.StartService();
- } else {
- echo "The service $ServiceName is already running on $ComputerName";
- }
- }
- } else { echo "No service named $ServiceName has been found."; }
- }
- # Stop a distant service
- function Stop-DistantService ([string] $ComputerName, [string] $ServiceName) {
- $aServices = Get-DistantService $ComputerName $ServiceName;
- if ($aServices -ne $null) {
- foreach ($oService in $aServices) {
- if ($oService.Started) {
- return $oService.StopService();
- } else {
- echo "The service $ServiceName is not running on $ComputerName";
- }
- }
- } else { echo "No service named $ServiceName has been found."; }
- }
- ################################################################
- ###################### Process Managment ######################
- ################################################################
- # Get one or more distant process on a distant computer by name (accepts * wildcards)
- function Get-DistantProcess ([string] $ComputerName, [string] $ProcessName) {
- $ProcessName = $ProcessName -replace '\*','%';
- $ComputerName = [System.Net.Dns]::GetHostAddresses("$ComputerName"[0]);
- return (gwmi -ComputerName $ComputerName -Query "SELECT * FROM win32_process WHERE Name LIKE '$ProcessName'");
- }
- # Start a process on a distant computer
- function Start-DistantProcess ([string] $ComputerName, [string] $ProcessName) {
- $ComputerName = [System.Net.Dns]::GetHostAddresses("$ComputerName")[0];
- return (iwmi –ComputerName $ComputerName -Class win32_process -Name create -ArgumentList "$ProcessName");
- }
- # Stop one or more processes on a distant computer (accepts * wildcards)
- function Stop-DistantProcess ([string] $ComputerName, [string] $ProcessName) {
- $oProcesses = Get-DistantProcess $ComputerName $ProcessName
- if ($oProcesses -ne $null) {
- foreach ($oProcess in $oProcesses) {
- try {
- $sName = $oProcess.Name;
- $iRetVal = $oProcess.terminate();
- $iPID = $oProcess.handle;
- } catch {
- $sName = "null";
- $iRetVal = "-1";
- $iPID = "-1";
- }
- if($iRetVal.returnvalue -eq 0) {
- Write-Host "The process $sName `($iPID`) terminated successfully"
- }
- else {
- Write-Host "The process $sName `($iPID`) termination has some problems"
- }
- }
- } else { echo "No process with this name has been found."; }
- }
- #################################################################
- ###################### System Informations ######################
- #################################################################
- # Get Operating System infos from a distant computer
- function Get-DistantSystemInfos ([string] $ComputerName) {
- $ComputerName = [System.Net.Dns]::GetHostAddresses("$ComputerName")[0];
- return (gwmi -ComputerName $ComputerName -Class Win32_OperatingSystem | Select-Object -Property *);
- }
- # Get the list of compressed files on a distant computer
- function Get-DistantCompressedFiles ([string] $ComputerName) {
- $ComputerName = [System.Net.Dns]::GetHostAddresses("$ComputerName")[0];
- return (gwmi -ComputerName $ComputerName -Class CIM_DataFile -Filter "Compressed = 'True'");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement