How to Copy files from Linux machine to Windows machine


Hi All,

Here is the SCP (secure copy) command line utility that we could use to copy the files from Linux machine (ubuntu)

Regards,

Chaitanya

Linux How to Copy files from Linux machine to Windows machine


Hi All,

Here is the SCP (secure copy) command line utility that we could use to copy the files from Linux machine (ubuntu)

scp username "E:/Work/Devops/Ansible/"

Regards,

Chaitanya

Server Latency Check Script


Hi Everyone,

Below is the snippet you could use to check the latency from given server to another servers.

$sourceServer=’localhost’

$Destinationserverlist=’localhost’,’CHAY-WIN10′

$PacketCount=4 #default

$FinalServerLatencyReport=@()

foreach ($server in $Destinationserverlist)

{

$FinalServerLatencyReport+= Test-Connection -Source $sourceServer -ComputerName $server -Count $PacketCount| `

Measure-Object ResponseTime -Maximum -minimum | select @{name=’Source Computer’;expression={$sourceServer}},`

@{name=’Target Computer’;expression={$server}}, @{name=’Packet Count’;expression={$_.count}},`

@{name=’Minimum Time(ms)’;expression={$_.Minimum}},@{name=’Maximum Time(ms)’;expression={$_.Maximum}}

}

$FinalServerLatencyReport|Out-GridView

Regards,

Chaitanya

Powershell Convert XLS file to CSV using Powershell


Hi All,

Below is the little snippet used to convert the Excel file to Csv format. Remember irrespective of how many sheets you had, after converting CSV file would have only one sheet

function ConvertFromExceltoCSV ( $Inputfilepath) {

$excel = New-Object -ComObject excel.application

$excel.visible = $false

Start-Sleep -Seconds 1

$fileOutputPath = $ Inputfilepath.Split(".")[0] + ".csv"

$reportOut = $excel.Workbooks.Open("$Inputfilepath ")

$reportOut.SaveAs("$fileOutputPath ", [Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSV)

$reportOut.Close(0)

$excel.Quit()

}

# Calling the function with Excel file as input by giving the location of excel sheets

Get-ChildItem -Path ‘C:\Users\Chaitanya\Documents\PS_Cert-Removal\Certs’ -Recurse | ForEach-Object -Process {

ConvertFromExceltoCSV – Inputfilepath $_.FullName

}

Regards,

Chaitanya

Retrieve IIS Client mapping certificates


Hi All,

Below is the snippet to retrieve the client mapping certificate from IIS .

$siteName = "WebSiteName"

# Get build folder path

$ScriptPath = $MyInvocation.MyCommand.Path

# Get build folder parent directory

$ScriptDir = Split-Path -Parent $ScriptPath

[string] $logdate =get-date -Format "yyyyMMddhhmm"

$OutputFolderPath = $ScriptDir + "\Certificates"

if(!(Test-Path $OutputFolderPath))

{

New-Item -ItemType directory -Path $OutputFolderPath

}

$OutputFolderFilepath =$OutputFolderPath + "\" + "$logdate"

if(!(Test-Path $OutputFolderFilepath))

{

New-Item -ItemType directory -Path $OutputFolderFilepath

}

$authentications = Get-WebConfiguration `

-filter "system.webServer/security/authentication/*" `

-PSPath "IIS:\Sites\$siteName"

foreach ($auth in $authentications)

{

if ($auth.sectionpath -like ‘*iisClientCertificateMappingAuthentication*’)

{

$IISMappings=$auth

}

}

$certCollection=$IISMappings|select -ExpandProperty oneToOneMappings|select -expandproperty collection

$count=$certCollection.Count;

for( $i=0; $i -le $count; $i++)

{

$cert= $certCollection[$i].certificate

$Certname="Cert"+$i

$certPath=$OutputFolderFilepath+"\$Certname.cer"

$cert| Set-Content -Path "$certPath" -Force

}

$certList = Get-ChildItem $OutputFolderFilepath

$IISCerts=@()

foreach ($cer in $certList)

{

$certPrint = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2

$certPrint.Import($cer.fullname)

# Write-Host $cer.Name,$certPrint.SerialNumber

$IISCerts +=[pscustomobject][ordered]@{CertificateName=$cer.Name

CertificateSerialNumber=$certPrint.SerialNumber

CertificateExpirydate=$certPrint.NotAfter

Subject=$certPrint.Subject

}

}

$IISCerts|?{$_.CertificateExpirydate -ge (Get-Date)}|Export-Csv -Path $OutputFolderFilepath -NoTypeInformation

regards,

Chaitanya

Delete Duplicate Images in the Folder


HI Everyone,

Often we end up having same image multiple times in the same folder. It is very hard to remove. Below script will help you in easing this task.

$Folder = “C:\Users\chaitanya\pictures\Testing”

$Folderobj =get-childitem -Path $Folder -Recurse |where { ! $_.PSIsContainer }

$FolderobjCount = $Folderobj.Count

$ImageObjArr = @();

$Folderobj| ForEach-Object {

$FolderobjCount–;

$ImageHash = $null

$ImageFileName = $_.Name

$ImageFilePath = $_.FullName

$ImageFileCreateDate = $_.CreationTime

$ImageHash = Get-FileHash -Path $ImageFilePath -Algorithm MD5

Write-Host “Creating Hash for Destination Image name $($ImageFileName) and Current Image Count is : $($FolderobjCount) ” -ForegroundColor cyan

if($ImageHash)

{

$ImageObjArr += [pscustomobject]@{ImageFileName=$ImageFileName;ImageFilePath=$ImageFilePath;ImageCreateDate=$ImageFileCreateDate; ImageFileHash=$($ImageHash).Hash;}

}

}

$DeletedImages = @();

$ImageObjArr | Group-Object -Property ImageFileHash | Where-Object {$_.count -gt ‘1’} | ForEach-Object -process{

$DuplicateImageObj = $_.group | Sort-Object -Property ImageCreateDate |select -Skip 1

if($DuplicateImageObj)

{

foreach($dup in $DuplicateImageObj)

{

Write-Host “Started Deleting the Duplicate Image name $($dup.ImageFileName) ” -ForegroundColor Yellow

$DeletedImages += [pscustomobject]@{ImageFileName=$dup.ImageFileName;ImageFilePath=$dup.ImageFilePath;}

get-childitem -Path $dup.ImageFilePath | Remove-Item -Force

Write-Host “Completed Deleting the Duplicate Image name $($dup.ImageFileName) ” -ForegroundColor Green

}

}

}

$DeletedImages |Out-GridView

Note: Please be careful while giving the folder that you want, it does recursive operation and search sub folders and delete the files too apart from Images.

Regards,

Chaitanya

Compare 2 Image folders and copy the missing images


Hi Everyone,

 

often , we do take photos and store it in our external drives  with same name or different name.

ex: we have inserted the  Camera memory card  in our system and copy it to our specific drive or to other external drive.  some time though images are same, file names might be different.  we can do the image comparison using powershell hash. below is the script for this.

 

$Source= “C:\Users\Chaitanya\Pictures\Camera Roll”
$Destination = “C:\Users\Chaitanya\Pictures\Test\Testing”

$Destinationobj =get-childitem -Path $Destination -Recurse

$DestinationobjCount=$Destinationobj.Count

$DestObjArr=@();
$Destinationobj| ForEach-Object {

$DestinationobjCount–;

$DestHash=$null
$DestFileName = $_.Name
$DestFilePath = $_.FullName
$DestHash = Get-FileHash -Path $DestFilePath -Algorithm MD5

Write-Host “Creating Hash for Destination Image name $($DestFileName) and Current Image Count is : $($DestinationobjCount) ” -ForegroundColor cyan

if($DestHash)
{

$DestObjArr+=[pscustomobject]@{DestFileName=$DestFileName; DestFileHash=$($DestHash).Hash;}

}

}

$SourceObj =get-childitem -Path $Source -Recurse

$SourceObjCount=$SourceObj.Count

$SourceObj| ForEach-Object {

$SourceObjCount–;

$SrcFile = $_.FullName
$SRCFileName = $_.Name

$DestFilepath =$Destination+”\”+$SRCFileName

Write-Host “Creating Hash for Source Image name $($SRCFileName) and Current Image Count is : $($SourceObjCount)” -ForegroundColor cyan

$SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5

if ($SrcHash)

{

Write-Host “Checking the Destination Hash for Source File Name : $($SRCFileName) ” -ForegroundColor Cyan

if ($DestObjArr | Where-Object {$_.DestFileHash -eq $($SrcHash).Hash})
{

Write-Host “Hash Exists for Source Image Name $($SRCFileName)” -ForegroundColor DarkYellow

}
else
{

Write-Warning “Hash does Not Exists for Source Image Name :$($SRCFileName), Copying ..”

if (!(Test-Path $DestFilepath))
{

Write-Host “File Name not exists, Copying started for Source Image Name :$($SRCFileName)” -ForegroundColor White

Copy-Item $SrcFile -Destination $DestFilepath

}
else
{

Write-Host “File Name exists, create new name, Copying started for Source Image Name :$($SRCFileName)” -ForegroundColor Magenta

[string]$logdate= Get-Date -Format “yyyyMMddHHssmm”

$DestFilepath =$Destination+”\”+$SRCFileName.Split(“.”)[0]+ $($logdate)+”.”+$SRCFileName.Split(“.”)[1]

Copy-Item $SrcFile -Destination $DestFilepath

}

Write-Host “Copying complete for Source Image Name :$($SRCFileName)” -ForegroundColor Green
}

}

}

 

 

i hope with this your problem with the image saving in your external drives will be solved.

 

post your comments and let us your opinion on this.

 

regards,

Chay

IIS Log file Location check Using PowerShell


Hi All,

if you want to check the IIS web site log location, You need to go to server and check the log file location.

Below is the simple power shell script, you could use on multiple servers and get the web sites Log file Location and latest log file name.

invoke-command -ComputerName Server1,Server2 -ScriptBlock {

Import-Module WebAdministration

$array =@()

foreach($WebSite in $(get-website))

{

$logFileLoc = "$($Website.logFile.directory)\w3svc$($website.id)".replace("%SystemDrive%",$env:SystemDrive)

$latestLogFileName =Get-ChildItem -path $logFileLoc |Sort-Object LastWriteTimeUtc -Descending |select -First 1

$array += [pscustomobject] [ordered] @{ # Only if on PowerShell V3

ServerName=$env:computername

WebsiteName = $($WebSite.name)

LogFileLocation = $logFileLoc

LatestLogFileName = $($latestLogFileName.BaseName)

}

}

$array

}| Select-Object * -ExcludeProperty RunspaceId, PSComputerName |Out-GridView

Regards,

Chaitanya

Finding Files in a Folder having Carriage return


Below is the simple powershell script to find Files in a Folder having Carriage return

Get-ChildItem -Path ‘\\fileshare\folder’ | ForEach-Object {

$contents = [System.IO.File]::ReadAllText($_.FullName)

if ($contents -cmatch ‘\r\n’) {

$_.BaseName

}

}

Happy scripting.

Regards,

Chaitanya

Delete Unwanted Host Instances on the specified BizTalk Server


<#

.SYNOPSIS

<Delete Unwanted Host Instances on the specified BizTalk Server >

.DESCRIPTION

< you can delete the host instances that are not using , because host instances cause CPU and Memory Consumption>

.EXAMPLE

<Open the script in power Shell ISE with Run as Admin permission in the Any one of the BizTalk Web server and run it>

#>

[ARRAY]$hostInstanceObj = get-wmiobject MSBTS_HostInstance -namespace ‘root\MicrosoftBizTalkServer’ -Filter ‘RunningServer="Server1"’

[ARRAY]$HostInstanceObj1 = "Process_Host2","Archive_Host" # Provide the Unwanted Host Instances that you want to remove on the Server1

$hostInstances = $hostInstanceObj | ?{$HostInstanceObj1 -contains $_.hostname}

[ARRAY]$HostInfo = get-wmiobject MSBTS_ServerHost -namespace ‘root\MicrosoftBizTalkServer’ -Filter ‘ServerName="Server1"’

foreach ( $hostInstance in $hostInstances)

{

if ( $hostInstance.GetState().state -eq ‘4’)

{

Write-Output " stopping the Host name $($hostInstance.hostname)"

$hostInstance.InvokeMethod("Stop",$null)

}

if ( $hostInstance.GetState().state -eq ‘1’)

{

Write-Output " Deleting the Host name $($hostInstance.hostname)"

$hostInstance.InvokeMethod("UnInstall",$null)

foreach ( $hostrem in $HostInfo )

{

if ( $hostrem.hostname -eq $hostInstance.hostname)

{

$host2 = $hostrem

$host2.InvokeMethod("UnMap",$Null)

Write-Output " Unmapped the Host $($hostrem.hostname)"

}

}

}

}

Disclaimer : Please test in Lower environments before using this production . it Deletes the Host Instances that you mention