by TechiBee
on January 31, 2013
Today I got a requirement to use Regular expressions in one of script. After some googling, I came across below Cheat Sheet which is very useful for understanding and using regular expressions in Powershell. I printed and pasted it at my desk for quick reference.
http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/pdf/
Hope you will also find this useful.
{ }
by TechiBee
on January 25, 2013
In today’s PSTIP# section will show you how to start a new process and get its PID using powershell. Sometimes we may get requirement to kill the process we started and in such cases knowing the PID will help us in easily killing the process. Unfortunately, Start-Process cmdlet in Powershell will not return the PID of the process after starting it. So we need to depend on [Diagnostics.Process] dotnet class for this purpose.
Code:
$Process = [Diagnostics.Process]::Start("notepad")
$id = $Process.Id
Write-Host "Process created. Process id is $id"
Write-Host "sleeping for 5 seconds"
Start-Sleep -Seconds 5
try {
Stop-Process -Id $id -ErrorAction stop
Write-Host "Successfully killed the process with ID: $ID"
} catch {
Write-Host "Failed to kill the process"
}
Hope this helps.
{ }
by TechiBee
on January 25, 2013
If you have a VMware environment and would like to use VMware Powershell CLI to get list of Snapshots of a given VM, use the below command.
Get-VM -Name "myVM1" | Get-SnapShot
This lists the snapshot name, description and the state of snapshot. Needless to say that you need to connect to you virtual center server before executing this command. Connection can be established by using
Connect-VIServer -Server MYVC1
Hope this helps.
{ }
by TechiBee
on December 6, 2012
Sometimes, we may want to know when a particular Powershell command was executed in our current shell. Simply scrolling through the powershell history will not help you as it just shows the commands. I thought having current date and time as part of poweshell prompt improves the situation to a greater level. It also helps you in recording your changes in proper way.
Here is a small piece of code which helps you to add current date time to powershell prompt. You can change the formatting of the date and time to meet your requirements. But make sure to keep the function name same
Code:
function prompt
{
"PS " + $(get-location) + " [$(Get-Date)]> "
}
Usage and Output:
You can clearly see in the above picture that, I simply copy pasted the code into powershell prompt and it started showing the date time stamp in the prompt section. The date and time are changing when switched to a new directory or simply pressed enter or when entering a command.
This is very helpful for tracking. Add this to your powershell profile to get this prompt every time you open powershell window.
{ }
by TechiBee
on December 6, 2012
In this post I will show you how to query for empty folders/directories in windows computer using powershell. Empty directory/folder means it will not have anything inside it. So our logic is to find child items inside each directory and see if it returns any objects or not. If we get any objects back, the folder is not empty otherwise, it is.
Here is the code and usage example:
FileName : Get-EmptyDirectories.ps1
[cmdletbinding()]
param(
[string]$DirectoryName
)
$Directories = Get-ChildItem -Path $DirectoryName -Recurse | ? {$_.PSIsContainer -eq $true }
foreach($Directory in $Directories) {
$Items = Get-ChildItem -Path $Directory.FullName
if(!$items) {
Write-Host "$($Directory.Fullname) is empty"
}
}
Output:
Read this post if you want to find empty or zero sized files in the file system.
{ }
by TechiBee
on December 6, 2012
In this post let see how to find out the older item(be it folder or file) in a given folder. This can be achieved by reading all files/folders in the given path using Get-ChildItem cmdlet and then sorting by CreationTime Property of each returned output. By default Sort-Object does sorting by ascending order. After sorting, the output is further filtered out using select statement and -First parameter to get the first value in the sorted list which is the oldest item in the folder.
Below is the code.
File Name: Get-OldestItemInFolder.ps1
[cmdletbinding()]
param(
[string]$FolderName
)
$Item = Get-ChildItem -Path $FolderName | Sort CreationTime | select -First 1
Write-Host "Oldest file/folder in $FolderName is $($Item.FullName)"
Write-host "Creation Time is $($item.creationtime)"
Output:
This script takes optional -FolderName parameter if you want to give a specific folder. Otherwise it will run against current directory from where the script is triggered.
{ }
by TechiBee
on November 30, 2012
Today I ran into a situation where my powershell process memory utilization increased to ~2GB. I know why it happened. I ran a Powershell script from the PS window which queries event log from a few computers. The event log data will be high in volume generally and since PS keeps this run time data in memory, the process memory utilization increased to such a high value. In ideal world, the memory should get freed up at the end of script execution, but it didn’t happen.
I can close my powershell process to clear the memory but that is a good solution. Moreover, I did some work in that console and would like to retain/review that(like history). After searching for sometime to find a solution finally I landed on System.GC (Garbage collection) class in Dotnet. It has a method to perform garbage collection for current powershell process. I used the below command to release the memory used by current powershell process.
[System.GC]::Collect()
Do you have any other better method to do this? Feel free to comment.
{ }
by TechiBee
on November 29, 2012
I came across a nice article today explaining various inside outs of vCPUs in VMware ESXi. It talks starting from how vCPUs are formed and its relation with physical CPUs, how multi-threading effects no. of vCPUs, and more importantly how over provisioned vCPUs can cause your Virtual machine to run slow compared with virtual machines with single vCPU.
http://www.virtualinstruments.com/sanbestpractices/best-practices/avoiding-the-virtual-cpu-dilemma-overprovisioning-vcpu-to-pcpu-ratios/
I feel its worth reading..
{ }
by TechiBee
on November 28, 2012
I have seen few people asking for a way to create folder or file with today’s date and time in name using powershell. Though it is easy, many people ask for this. Given the demand, I am authoring this quick post.
Create folder using todays date and time:
$folderName = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
New-Item -itemType Directory -Path c:\scripts -Name $FolderName
Executing the above code will create a folder with current date and time in c:\scripts folder. You can choose to adjust the format of date time in the above code.
Create file using todays date and time:
$FileName = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
New-Item -itemType File -Path c:\scripts -Name ($FileName + ".log")
Hope this tips helps you.
{ }
by TechiBee
on November 16, 2012
The code provided in this post will help you to get the target folder configured for a symlink. This the folder to which your symlink will redirect(or I should say proxy) when you access the symlink folder.
function Get-SymlinkTaregetDirectory {
[cmdletbinding()]
param(
[string]$SymlinkDir
)
Add-Type -MemberDefinition @" private const int FILE_SHARE_READ = 1;
private const int FILE_SHARE_WRITE = 2;
private const int CREATION_DISPOSITION_OPEN_EXISTING = 3;
private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
[DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetFinalPathNameByHandle(IntPtr handle, [In, Out] StringBuilder path, int bufLen, int flags);
[DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess,
int dwShareMode, IntPtr SecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
public static string GetSymbolicLinkTarget(System.IO.DirectoryInfo symlink)
{
SafeFileHandle directoryHandle = CreateFile(symlink.FullName, 0, 2, System.IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, System.IntPtr.Zero);
if(directoryHandle.IsInvalid)
throw new Win32Exception(Marshal.GetLastWin32Error());
StringBuilder path = new StringBuilder(512);
int size = GetFinalPathNameByHandle(directoryHandle.DangerousGetHandle(), path, path.Capacity, 0);
if (size<0) throw new Win32Exception(Marshal.GetLastWin32Error()); // The remarks section of GetFinalPathNameByHandle mentions the return being prefixed with "\\?\" // More information about "\\?\" here -> http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx
if (path[0] == '\\' && path[1] == '\\' && path[2] == '?' && path[3] == '\\') return path.ToString().Substring(4);
else
return path.ToString();
} "@ -Name Win32 -NameSpace System -UsingNamespace System.Text,Microsoft.Win32.SafeHandles,System.ComponentModel
[System.Win32]::GetSymbolicLinkTarget($SymlinkDir)
}
Usage:
Get-SymlinkTargetDirectory -SymlinkDir c:\mytemp
Hope this helps.
{ }