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

Advertisements

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

PowerShell Script to Start Only Specified Host instances and Stop remaining Host Instances in BizTalk server


Hi All,

Some of the times, we are in a situation to start only specified host instances in your biztalk group and stop the remaining . in that case, you could use this script to achieve that.

<#

.SYNOPSIS

<This is the Biztalk Server product related Script, you could use this in the

biztalk server Environment for starting only specified hosts and stop another Host Instances

>

.DESCRIPTION

<Used to Start the specified Host Instances on your BizTalk environment and stop the remaining Hosts >

.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’

[ARRAY]$HostInstanceObj1 = "BizTalkServerApplication","TrackingHost"

$StophostInstances = $hostInstanceObj | ?{$HostInstanceObj1 -notcontains $_.hostname}

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

$StophostInstancesFinal = $StophostInstances|?{$_.HostType -eq ‘1’}

foreach ( $hostInstance in $StophostInstancesFinal)

{

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

{

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

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

}

}

foreach ( $stInstance in $starthostInstances)

{

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

{

Write-Output " Starting the Host name $($stInstance.hostname) on Server $($stInstance.RunningServer)"

$stInstance.InvokeMethod("Start",$null)

}

}

Disclaimer : Please test in Lower environments before using this production . it stop the other host instances that you would not mention.

Regards,

Chaitanya

Want Copy Files From Sql Result as you want


Hi All,

Often we try to copy the files from sql server tables and we end up renaming the copied file names manually.

Here is simple sql server statement, where it pull the message archive location and name of the file will be with some GUID and you want to name with PO number column , use this simple select statement

select ‘copy ‘+MessageArchiveLocation +’,’+’"’+PONumber+’.xml’+’"’ from Ordertransactions (nolock) where PONumber in

(‘12344′,’112112′,’11231232’)

order by 1 desc

you will get the results, then copy them to batch file and run it.

Regards,

Chaitanya

Delete GUID named files in folders


Hi All,

Want to delete the GUID named files in your folders?

Here is the simple script that takes the regex for GUID and compares the file name and remove it.

get-childitem |? {$_.basename -match("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")} |Remove-Item

if you want to Include subfolders too, use the recurse option like below

get-childitem -Recurse |? {$_.basename -match("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")} |Remove-Item

Note: please test it in lower environments before implementing in production.

Regards,

Chaitanya

check .Net frame work Version for the DLLs


Hi All,

If you got bunch of DLLs and want to check the .net version for those, this is simple script you can use to find out the version.

# check .net frame work for the DLLs

$filepath=’D:\Builds\\AutomatedDeployment\Deployment\Add-PreDlls’

$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"

}

Happy scripting….

Regards,

Chaitanya

Configure schedule task that runs every 5 minutes for N years


Hi all,

Here is the sample PowerShell script to configure schedule task that runs every 5 minutes for 3 years.

You can change the years , why did I not mentioned infinite time is because of some limitation that I had explained in the other article.

https://sqlblogging.com/2017/08/16/fixethe-task-xml-contains-a-value-which-is-incorrectly-formatted-or-out-of-range-durationp99999999dt23h59m59s/

Script:

$mycredentials = Get-Credential

Invoke-Command -ComputerName “Server1”,”Server2” -Credential $mycredentials -ScriptBlock {

$dt= ([DateTime]::Now)

$timespan = $dt.AddYears(3) -$dt;

$Action = New-ScheduledTaskAction -Execute ‘powershell.exe’ -Argument ‘-command “D:\PS_Jobs\PS_Job1.ps1” -ExecutionPolicy RemoteSigned -NoProfile’

$Trigger = New-ScheduledTaskTrigger -Once -At 9am -RandomDelay (New-TimeSpan -Minutes 30) -RepetitionDuration $timespan -RepetitionInterval (New-TimeSpan -Minutes 60)

$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet)

$Task | Register-ScheduledTask -TaskName ‘Job1 Task’

}

Regards,

Chaitanya

Convert datetime::Now value to specific format for comparison-Power Shell


Hi All,

How to convert datetime value to specific format for comparison

For get-date cmdlet, we have property called format , using that I can convert the date into specific format.

But for [datetime]::Now as it is c# static class, for that to convert the date time into specific format, you could use the toString method like below

$date = get-date -Format "yyyyMMddhhmm" – input value

$date2=([datetime]::Now).ToString("yyyyMMddhhmm") -comparing against time

if ( $date -eq $date2)

{

"yes"

}

else

{

"no"

}

Regards,

Chaitanya

Block Chain Technology – the Next Internet


As you are hearing now a days about lot about bit coin and its value reaching all-time high, but Bit coin actually works using block chain model.

Most of them predicting this is the new internet and also it is a new technology revolution.

This is the TedEx video , you could find the information on Block chain in a simplified way. I could not agree more on that.

https://www.youtube.com/watch?v=k53LUZxUF50

Please watch it and welcome to new technology advancement.

regards,

Chaitanya

%d bloggers like this: