Monday, January 9, 2012

Creating Shares Remotely using Powershell

 

PowerTip of the Day, from PowerShell.com:

 

Let's assume you need to access another machine's file system but there is no network share available. Provided you have local administrator privileges and WMI remoting is allowed in your Firewall, here is a one-liner that adds another share remotely:

PS> ([wmiclass]'\\storage1\root\cimv2:Win32_Share').Create('c:\', 'Hidden', 0, 12, 'secret share').ReturnValue

Just replace "storage1" with the name or IP address of the system you want to access. Note that this method does not allow for separate authentication, so your current user must have local administrator privileges on the target machine.

A return value of 0 indicates success. If you receive a "2", you do not have proper permissions.

thanks

Opening MsgBoxes using PowerShell

 

PowerTip of the Day, from PowerShell.com:

 

eed a quick message box to display something or ask a question? Fortunately, PowerShell can access old COM components. Here's a line that creates a MsgBox for 5 seconds. If the user does not make a choice within that time, it returns -1: a perfect solution for scripts that need to run unattended if no one is around.

$msg = New-Object -ComObject WScript.Shell

$msg.Popup("Hello", 5, "Title", 48)

To find out more about the Popup() method and its arguments, visit:

http://msdn.microsoft.com/en-us/library/x83z1d9f(v=VS.84).aspx

This link documents all the other WSH scripting methods as well

 

Thanks

Spying on Parameters in Powershell

 

PowerTip of the Day, from PowerShell.com:

 

Your own PowerShell functions can have the same sophisticated parameters, parameter types and parameter sets that you know from cmdlets. However, it is not always obvious how to construct the param() block appropriately.

A clever way is to spy on cmdlets and look how they did it. For example, if you wonder how Get-EventLog made its -LogName parameter mandatory, check out its param() block:

PS> $cmd = Get-Command -commandType cmdlet Get-EventLog

PS> [System.Management.Automation.ProxyCommand]::GetParamBlock($cmd)

 

    [Parameter(ParameterSetName='LogName', Mandatory=$true, Position=0)]

    [Alias('LN')]

    [System.String]

    ${LogName},

 

    [Alias('Cn')]

    [ValidateNotNullOrEmpty()]

    [System.String[]]

    ${ComputerName},

 

    [Parameter(ParameterSetName='LogName')]

    [ValidateRange(0, 2147483647)]

    [System.Int32]

    ${Newest},

(...)

Not only will you discover that the keyword Mandatory=$true made -LogName mandatory. You also see all the hidden parameter aliases as well as validator attributes. The parameter -Newest for example will only accept a range between 0 and 2147483647, and your own parameters could do the same now, too

Thanks

Sending Text to Clipboard Everywhere using Powershell

In a previous tip you learned how to use clip.exe to send results to the clipboard. But what if you don't have clip.exe (let's say on Windows XP) or don't want dependencies?

Here's a clever alternative:

function Out-Clipboard {

 param(

  $text

 )

 Add-Type -AssemblyName System.Windows.Forms

 $tb = New-Object System.Windows.Forms.TextBox

 $tb.Multiline = $true

     

 if ($Input -ne $null) {

  $Input.Reset()

  $tb.Text = $Input | Out-String

 } else {

  $tb.Text = $text

 }

 $tb.SelectAll()

 $tb.Copy()

}

Use it like this:

PS> Get-Process | Out-Clipboard

It solely uses .NET Framework functionality that is available in all versions and modes of PowerShell

Reading the Clipboard using Powershell

 

PowerTip of the Day, from PowerShell.com:

 

What if you wanted to paste information from the clipboard? No sweat, here is a Get-Clipboard function that outputs any text held by the clipboard:

function Get-Clipboard {

 Add-Type -AssemblyName System.Windows.Forms

 $tb = New-Object System.Windows.Forms.TextBox

 $tb.Multiline = $true

 $tb.Paste()

 $tb.Text

}

In a previous tip we presented the corresponding Out-Clipboard function, so now you could send information to the clipboard in one PowerShell session and read it back from another.

Thanks

Aman

Bulk-Creating PDF Files from Word

 

PowerTip of the Day, from PowerShell.com:

 

To convert a whole folder full of MS Word documents to PDF, here's a function that might help:

function Export-WordToPDF {

  param(

  [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]

  [Alias("FullName")]

  $path,

  $pdfpath = $null)

  process {

    if (!$pdfpath) {

      $pdfpath = [System.IO.Path]::ChangeExtension($path, '.pdf')

    }

    $word = New-Object -ComObject Word.Application

    $word.displayAlerts = $false

   

    $word.Visible = $true

    $doc = $word.Documents.Open($path)

    #$doc.TrackRevisions = $false

    $null = $word.ActiveDocument.ExportAsFixedFormat($pdfpath, 17, $false, 1)

    $word.ActiveDocument.Close()

    $word.Quit()

  }

}

Use it like this:

PS> Dir c:\folder -Filter *.doc | Export-WordToPDF

Thanks

Retrieve Exchange Rates using PowerShell

 

PowerTip of the Day, from PowerShell.com:

If you need up-to-date exchange rates, try loading the rates via XML from the European Central Bank. This sample gets you the latest exchange rates for USD-EUR conversion. It also has a link to other currency data. Just exchange the data link. We included a sample for Danish currency as well.

#Exchangerate feeds : http://www.ecb.int/home/html/rss.en.html

$url = 'http://www.ecb.int/rss/fxref-usd.html'

#$url = 'http://www.ecb.int/rss/fxref-dkk.html'

$xml = New-Object xml

$xml.Load($url)

$xml.RDF.Item |

      ForEach-Object {

            $rv = 1 | Select-Object Date, Currency, Rate, Description

            $rv.Date = [DateTime]$_.Date

            $rv.Description = $_.description.'#text'

            $rv.Currency = $_.statistics.exchangeRate.targetCurrency

            $rv.rate = $_.statistics.exchangeRate.value.'#text'

            $rv

      }

And this is what the output looks like (provided you have Internet access and no proxy is required):

Date                                          Currency Rate       Description

----                                          -------- ----       -----------

01.12.2011 14:15:00 USD               1.3492 1 EUR buys 1.3492 US dollar (USD) - The
...

30.11.2011 14:15:00 USD               1.3418 1 EUR buys 1.3418 US dollar (USD) - The
...

29.11.2011 14:15:00 USD               1.3336 1 EUR buys 1.3336 US dollar (USD) - The
...

28.11.2011 14:15:00 USD               1.3348 1 EUR buys 1.3348 US dollar (USD) - The
...

25.11.2011 14:15:00 USD               1.3229 1 EUR buys 1.3229 US dollar (USD) - The
...

 

Thanks