≡ Menu

I can not right away tell you the use cases of this procedure, but felt interesting. You can know the password entered through Get-Credential cmdlet.

$mycredentials = Get-Credential
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($mycredential.Password))

I got this from http://www.roelvanlisdonk.nl/?p=1150

Happy Learning..,
Sitaram Pamarthi

{ 0 comments }

Handling WMI errors in PowerShell

Today I worked on writing a script and in that I got a requirement to suppress errors. At the same time, I want to log the error description for my referrence. To achieve this, I made use of two parameters of Get-WmiObject cmdlet. They are, -ErrorVariable and -ErrorAction

In brief, the argument passed to -ErrorVariable stores the details of the error generated and -ErrorAction specifies what to do incase of errors, like whether to continue execution or stop or whatever action. Google search will make you to land in few beautiful sites which has more info about these two parameters. Alternately, you can run “Get-Help about_commonparameters”

OK, now lets see how we can suppress and capture the errors from WMI execution. Below command runs against a computer which doesn’t exists. I don’t want my script to fail there and it should continue by logging the error somewhere.

Get-WmiObject -Class Win32_Product -ComputerName nonexists -ErrorVariable myerror -ErrorAction SilentlyContinue

Above command will fail in real, but it will not show any error in screen. Error will be saved in $myerror object. To see the error, issue below command

$myerror[0].exception

To clear the error from object, use below

$myerror.clear()

To see how many errors are stored in this object, use below

$myerror.count
[/code]
You can use this in your scripts to avoid powershel printing errors in console and log to some file. Hope this helps.

{ 3 comments }

Use the script at https://techibee.com/powershell/powershell-script-to-query-softwares-installed-on-remote-computer/1389 to get installed software. Using Win32_Product has it’s own set of problems. 

I have seen my colleague writting multi line script to do subjected task in powershell today. Scripts works great but felt why can’t we do it with single line code. After messing up with options, I ended at below command.

Get-WmiObject -Class Win32_Product -Computer <remote-comp-name> | Sort-object Name | select Name

Looks cool and simple right? This command lists all the application installed in a given machine.

Now lets extend the functioanality to check if a particular application exists or not…and this can be done with single command again!!.

Get-WmiObject -Class Win32_Product | sort-object Name | select Name | where { $_.Name -match “Office”}

Happy Learning..,
Sitaram Pamarthi

{ 6 comments }

Lock your workstation using PowerShell

One other nice thing I found with PowerShell is it’s ability of using APIs by importing DLLs. Below is one example, where user32.dll is imported and LockWorkstation Function is invoked to lock the desktop. Btw, I grabbed this script from TechNet Library

Function Lock-WorkStation {
$signature = @”
[DllImport(“user32.dll”, SetLastError = true)]
public static extern bool LockWorkStation();
“@

$LockWorkStation = Add-Type -memberDefinition $signature -name “Win32LockWorkStation” -namespace Win32Functions -passthru
$LockWorkStation::LockWorkStation() | Out-Null
}

After executing above code, invoking “Lock-WorkStation” command from PowerShell window will lock your PC.

Happy Learning..,
Sitaram Pamarthi

{ 5 comments }

How to force reset a guest VM in ESX environment

Often we fall into a situation where resetting VM from Virtual Center console won’t help and server remains hung. In such cases we need to “hard” reset the VM to get it back online. 

To hard reset a VM, there are two options…

Option 1:

  1. SSH to host as root where VM is hosted
  2. Issue “vmware-cmd -l” to list the guests on the current host
  3. In output you will see VMX file location for each guest
  4. Now issue “vmware-cmd <path_of_guest_vm_vmx_file_that_you_want_to_restart> stop hard”
  5. If above command is successful, start the VM either from VC console or with command “vmware-cmd path_of_guest_vm_vmx_file_that_you_want_to_restart> start” 

Option 2:

In most cases, it option#1 should work. If it throws errors for some reason, try below method.

  1. SSH to host as root where VM is hosted
  2. Issue “vm-support -x”
  3. Result will give vmid(s) of guests running on the current host
  4. Now issue “vm-support -X 1234” where 1234 is the vmid of guest which you want to force reset
  5. Answer all the questions — mostly yes for all, but use your won intelligence
  6. This process will generate some debug information, so I prefer to execute this command from /tmp location.
  7. Once execution is completed, either use step#5 in option#1 or Virtual Center to start your guest VM.

Hope this helps…

{ 0 comments }

Quick way to create large dummy files

Many times during my operations, I came across a requirement to create a file of specific size. In earlier times, the requirement was to create a dummy file of size in few MBs. That dumb procedure I used to follow was copy pasting images into word document and save it as desired file. But today my requirement is to create 1GB file and this time I feared about copy pasting the files as it is really waste of efforts. I just looked around in google and came across a fantastic way to my job. Here it is…

fsutil file createnew c:\tempmyfile.txt (1gb)

Wow..it created the file in few seconds time and my requirement is served with less efforts. There are still people around us who depend on their own cum dump techniques to create such large files for their requirements. I thought of making a post for helping such people, and this page born.

Happy Learning..,
Sitaram Pamarthi

{ 2 comments }

I am working on testing a specific functionality in Windows 7 today and as part of that I have to clear “Application” event viewer every time I repeat the test. After running the test for few times, I got vexed up to open eventvwr to clear it manually from GUI. I quickly looked out for powershell option to clear event viewer and ended with below command.

Clear-EventLog -LogName Application

Look at the syntax of Clear-EventLog cmdlet to know more about it’s options.

I saved this command as ps1 and kept it on my desktop. Now I am able to clear event viewer by single mouse click..hey hey.. 🙂

Happy Learning..,
Sitaram Pamarthi

{ 0 comments }

Upload videos to Youtube using PowerShell

Do you think I am crazy..? Not me it’s Trevor who had this requirement and had written a script to do his task. It really sounds cool right? I love powershell only because of its capability of working with .Net components which makes many tasks possible in the world of IT administration.

You read Trevors post to know how to upload YouTube videos using PowerShell. If you want to send a email with GMAIL account using PowerShell, read one of my previous post.

{ 0 comments }

[Last updated on 07/05/2010]

A article on working with powershell remoting is available at https://techibee.com/powershell/administering-remoting-in-windows-7-powershell/541

Powershell.com has released a guide for administering Remoting in PowerShell. It deals wtih A-Z of remoting, various concepts, how to enable it, disable it, creating sessions for frequent access, etc, etc.

I love this guide because of the depth of content it covered. Don’t miss to download your copy from here.

Happy Learning..,
Sitaram Pamarthi

{ 0 comments }

Open/Close cd drive from command prompt

There is no windows built-in command to do this, but you can make use of a third party utility called nircmd.exe for this purpose. Download a copy of it from http://nirsoft.net and execute the below commands

To open …

nircmd.exe cdrom open D:

To Close …

nircmd.exe cdrom close D:

Where d: is your cd drive letter.

Happy Learning..,
Sitaram Pamarthi

{ 2 comments }