Create shortcut using powershell

Hi,

Can anyone tell me a wat to create RDP shortcuts using powershell?

I have written a script for mountpoints where in it compares the mountpoints with the mounpoints in the text file

for Ex:-

 $volume= get-wmiObject -class win32_volume -computername $server | Where-object{$_.DriveLetter -eq $null} | select label, name, Freespace

$volume gives me C:\Yo-01

$test="C:\Y0-01"

if ($volume -contains $test)

{

return (Pass) } else {return (Fail)}

when i execute, i get pass not  Fail though the mountpoints names are different. And please let me know how to create shortcuts as well?

July 1st, 2013 1:22pm

If you can use search engines you can find it easely. I have googled for you:

http://everydaynerd.com/microsoft/create-multiple-rdp-files-with-powershell

http://poshcode.org/674

You cannot use -contains with an PowerShell Object!

This should work (untested):

 $test="C:\Y0-01"
 
 Get-WmiObject -class win32_volume -computername $server | Where-object{$_.DriveLetter -eq $null} | ForEach-Object {
    If ($_.Name -Like $test) {
		"Volume $($_.Name) found!"
	} Else {
		"Volume $($_.Name) NOT found!"		
	}
} # end ForEach-Object		
Free Windows Admin Tool Kit Click here and download it now
July 1st, 2013 2:34pm

Thanks, but it doens't work.

I get all the blank values for mountpoints.

One more thing, i would like to add is i am pulling the mountpoints for comparsion from a text file so it takes each value as an array.

July 1st, 2013 3:54pm

I dont have mount point with directory here to test.

So i have to help you blind !

untested!

# remove this line if you run with a test.txt file!
$Testers = @("C:\Y0-01")


#example to read test volume-names from file
# $Testers = Get-Content 'C:\My\amazing\file\test.txt' # <<<< uncomment line !  


 # Get all mount points
 Get-WmiObject -class win32_volume -computername $server | ForEach-Object {
    
    # the current object in the pipeline is contained in the variable named $_ !
    $CurrentVolume = $_

    # flag for the volume test message
    $VolumeFound = $False

    # for each entry in the test.txt file we test the current volume
    ForEach($Test in $Testers) {

        If ($CurrentVolume.Name -Like "*$test*") {
		    # display found message
            ">>> Volume $($CurrentVolume.Name) found! <<<"
            # Set Flag to $True to supress not found message
             $VolumeFound = $True
            # Display found volume
            $CurrentVolume
	    }
    } # end ForEach ($Test in $Testers)

    # If $VolumeFound is $False we dont have found a matching volume
    If(-not $VolumeFound) {
        "Volume $($CurrentVolume.Name) NOT found!"
    }

} # end ForEach-Object
Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 8:04am

Thanks for the reply.

In between, can you tell how to catch error handling in Powershell.

i have tried many methods for catching the error, but none works.

 $_.Exception.GetType()

 $_.Exception.Message

nothing works.

it doesn;t work. Can you also tell how to catch "rpc server is unavailable exception from hresult" ?


Try
    { 
    
	   Write-Verbose "timezone check - Started"
	   $timezoneobj= get-wmiobject win32_timezone -computername $server 
	   $time= $timezoneobj.Caption
	   return ($time)
    } 
    Catch
    {
      return ('Failed to access "{0}" : {1} in "{2}"' -f $server, $_.Exception.Message)
     
    }
} 

July 2nd, 2013 9:51am

Hi can anyone help?

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2013 12:14am

Hi,

Give this a try:

Try { 

    $server = ""
    Write-Verbose "timezone check - Started"
    $timezoneobj= get-wmiobject win32_timezone -computername $server -ErrorAction Stop
    $time= $timezoneobj.Caption
    return ($time)
} 

Catch { return ('Failed to access "{0}" : {1}' -f $server, $_.Exception.Message) }

I added in an empty $server variable for you to fill in for testing and removed the unused {2} from the catch block.

EDIT: Here's some additional information on the topic:

http://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell

July 3rd, 2013 12:56am

What s about your initial question?
Please dont confuse  this thread! Stay tuned!

If you have new questions do a new Post!

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2013 2:31am

What s about your initial question?
Please dont confuse  this thread! Stay tuned!

If you have new questions do a new Post!

July 3rd, 2013 7:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics