finding shared folders on remote servers Using PowerShell
Use the below snippet code to check the shared folders on remote servers
#Check the shared folders on remote servers.
invoke-command -computername Server1,Server2 -ScriptBlock{ Get-SmbShare}
Regards,
Chaitanya
Gacing .Net 2.0 DLLs using GACutil and PowerShell
Use the below snippet code to GAC .net 2.0 DLLs using Combination of GACutil and PowerShell.
$filepath=’D:\DLLS\’
$files=Get-ChildItem -Path $filepath -Recurse -filter *.dll
$gacUtilLocation = "D:\gacutil\gacutil.exe"; # give the GACutil exe Location
$files | foreach-object {
Write-Host “Adding” $_.FullName
$asm = [System.Reflection.Assembly]::LoadFile($_.FullName)
$command = “&`”{0}`”/nologo /i `”{1}`”” -f $gacUtilLocation,$_.FullName
invoke-expression $command
}
Regards,
Chaitanya
Identify .Net Framework Version of DLLs using PowerShell
Use the below snippet code to check the version of the DLLs.
$filepath=’D:\DLLS\’
$files=Get-ChildItem -Path $filepath -Recurse -filter *.dll
foreach($file in $files)
{
$version = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($file.FullName).ImageRuntimeVersion;
echo "$($file) is using Framework: $version"
}
Regards,
Chaitanya
Searching Event Viewer Logs on Multiple servers Parallelly
Often if any issue happens, then we have to check what happened to service by checking the event viewer logs.
We can check the event viewer log data by searching keywords using below command
Get-EventLog -LogName $Appname -Newest 10 -Message "*$EventMessage*"
You can do it in multiple servers using invoke-command
Invoke-command -computername server1,server2 -scriptblock{Get-EventLog -LogName $Appname -Newest 10 -Message "*$EventMessage*" }
It internally uses PowerShell remoting.
You can use the below method to search data . but it uses dcom protocol to connect and search data
Get-EventLog -ComputerName $Server -LogName $Appname | Where-Object { $_.Message -like "*$($EventMessage)*"}|select -First 10 *
What if we want to search servers parallelly.
workflow parallelEventCheck {
param(
[String[]]$ComputerName,
[string]$EventMessage,
[string[]]$ApplicationList
)
foreach –parallel ($Appname in $ApplicationList)
{
inlinescript{
Get-EventLog -LogName $Using:Appname -Newest 10 -Message "*$Using:EventMessage*"
}
}
}#parallelEventCheck
parallelEventCheck -PSComputerName `
Server1,Server2
-EventMessage Error `
-ApplicationList ‘Application’,’System’ |out-gridview
The DCOM will run 5 in parallel by default.
The WSMAN will run 32 in parallel by default.
Using DCOM Method:
workflow parallelEventCheck {
param(
[String[]]$ComputerName,
[string]$EventMessage,
[string[]]$ApplicationList
)
foreach –parallel ($CN in $ComputerName)
{
Write-Warning "Computer is $CN"
foreach –parallel ($Appname in $ApplicationList)
{
Write-Warning "AppName is $AppName"
InlineScript
{
Get-EventLog -LogName $Using:Appname -Newest 100 -Message "*$Using:EventMessage*" -CN $Using:CN |
Select MachineName,* -EA 0
}
}#ApplicationList
}#Computername
}#parallelEventCheck
parallelEventCheck -ComputerName Server1, Server2-EventMessage error -ApplicationList System
Regards,
Chaitanya
Bulk installing MSIs in a server Using Powershell
Hi All,
Below is the PowerShell code for Bulk installing MSIs in a server
$MSIArray=get-childitem -Path ‘D:\MSIs\’ -Recurse -Filter *.msi # Folder where your MSIs present.
$LogFilepath=”D:\MSIs\Logs’
if(!(Test-Path $LogFilepath))
{
New-Item -ItemType directory -Path $LogFilepath
}
foreach ( $MSI in $MSIArray)
{
$MSISourcePath=$MSI.fullname
$AppName=$MSI.BaseName
$AppLogFilepath=$LogFilepath+"\"+$AppName+".txt"
$Ilist =
@(
"/I `"$MSISourcePath`"", # Install this MSI
"/QN", # Quietly, without a UI
"/L*V `"$AppLogFilepath`"" # Verbose output to this log
)
$InStallStatus =Start-Process -FilePath "msiexec" -ArgumentList $Ilist -Wait
}
You can install the MSIs by executing the above script.
Regards,
Chaitanya
Creating Folder Structure based on Existing Directory Structure
Hi All,
Below is the PowerShell code for Creating Folder Structure based on Existing Directory Structure
workflow parallelCopy {
param([string]$from,[string]$to_dest,[string[]]$ExcludeContent)
$DirArr = @()
$DirArr = Get-ChildItem -Path $from -Exclude $ExcludeContent
foreach –parallel ($dir in $DirArr)
{
inlinescript{
Write-Host "Creating Folder: $($Using:dir.Name)"
Copy-Item $($using:dir.fullname) $using:to_dest -Recurse -Force -Exclude $using:ExcludeContent
}
}
}
$SourceDirectory = Read-Host "Please enter Source Folder"
$DestDirectory = Read-Host "Please enter Destination Folder"
$ExcludeContent = @("*.txt","*.edi","*.PS1","*.xml","*.Zip",”Folder2”) # exclude the folders and file types if you don’t want that
parallelCopy -from $SourceDirectory -to_dest $DestDirectory -ExcludeContent $ExcludeContent
it will create the folders parallelly as we use work flows here.
Regards,
Chaitanya
Exporting base64 certificates based on thumbprint from Certificate Store
Hi All,
Below is the PowerShell code to extract the base64 certificates based on thumbprint
cd Cert:\CurrentUser\my
$Cert = Get-ChildItem | where{ $_.Thumbprint -eq "thumbprint" }
$out = New-Object String[] -ArgumentList 3
$OutputFile = "C:\Work\Base64Certificate.CER"
$out[0] = "
Logging Function for Scripts
Hi All,
We often log the errors/information warning while running the scripts in PowerShell. If we have the function , then it is better to call them where ever we need it in script instead of repeatedly adding the logging code.
## function for writelogs
function WriteLogs($LogMessage, $LogType)
{
$LoggingDateTime=get-date;
write-host
"$LogType, ["+
$LoggingDateTime
+"]:"+ $LogMessage | Add-Content -Path $LogFilepath
}
You can call this function like below
WriteLogs "***Installation failed: *** " "Error"
Regards,
Chaitanya
Closing Notepad Application in Multiple Servers
Hi All,
Often some users opens notepad applications in servers and they don’t save it and they do it for temporary data usage. We can close them with one PowerShell script in multiple servers.
Below is the PowerShell script for that.
For local :
Get-Process notepad | ? { $_.CloseMainWindow() | Out-Null }
For remote server:
invoke-command -ComputerName Server1,server2 -ScriptBlock{
Get-Process notepad | ? { $_.CloseMainWindow() | Out-Null }
}
Caution : it would close all the unsaved notepads. Please make sure before proceeding further.
Regards,
Chaitanya
Check Services which are in StopPending State in Multiple Servers
Hi All,
Below is the PowerShell script for that.
## check the services in Servers which are in state stop pending state
$computers = "server1","server2"
$array =@()
foreach ($computer in $computers)
{
$obj =New-Object psobject
$Services = Get-Service -ComputerName $computer | where { $_.Status -eq “stoppending” }
Add-Member -InputObject $obj -MemberType NoteProperty -Name Servername -Value $computer -Force
Add-Member -InputObject $obj -MemberType NoteProperty -Name Services -Value $Services -Force
$array+=$obj
$obj=$null
}
$array| select servername, services|select -ExpandProperty services|select machinename,name
Regards,
Chaitanya