Monday, 9 July 2012
Execution Policy
While a necessary evil, Execution Policy can sometimes be a pain.
Here's a couple of alternatives to setting the execution policy to Unrestricted:
Here's a couple of alternatives to setting the execution policy to Unrestricted:
Powershell.exe –noprofile –executionpolicy bypass –file "C:\Scripts\myScript.ps1"
Get-Content 'C:\Scripts\myScript.ps1' | powershell.exe -noprofile -
(The minus (-) is important!)
Wednesday, 4 July 2012
SSH.Net - Powershell
Thanks to SSH.Net lib, you can easily SSH into a linux server from powershell, upload/download files and run commands:
The following will create a folder in the users home called TestFolder, upload a file to that folder, then runs a command deleting teh folder:
The following will create a folder in the users home called TestFolder, upload a file to that folder, then runs a command deleting teh folder:
param( [parameter(Mandatory = $true)] [string] $serverName, [parameter(Mandatory = $true)] [string] $userName, [parameter(Mandatory = $true)] [string] $password ) [Void][Reflection.Assembly]::LoadFrom(".\Renci.SshNet.dll") $sourceFile = "c:\Scripts\test.txt" $destFile = "test.txt" $destFolder = "/home/$userName/TestFolder" $sshclient = New-Object Renci.SshNet.SshClient($serverName,$userName,$password ) $sshclient.Connect() if ($sshclient.IsConnected){ $sftp = New-Object Renci.SshNet.SftpClient($sshclient.ConnectionInfo) $sftp.Connect() if ($sftp.IsConnected){ if (!$sftp.Exists($destFolder)){ $sftp.CreateDirectory($destFolder) } $sftp.ChangeDirectory($destFolder) $sftp.UploadFile([System.IO.File]::OpenRead($sourceFile), $destFile) write-host "File Exists " $([bool](($sftp.ListDirectory(".") | select Name) -match "test.txt")) $sftp.Disconnect() } $o = $sshclient.RunCommand("rm /home/$userName/TestFolder -R") $o.Result $sshclient.Disconnect() }
Match string between same delimiters multiple times
Been looking for something like this for a while...
I had a string similar to below:
"thisis%a%%test%string%which%should%will%possibly%work%"
and needed to extract the text between the "%"'s, but not between the end "%" and the next starting "%".
Using regular expressions (in powershell), the following pattern will work:
"%[^%]*?(|).*?%"
The "?" after each "*" allows for greediness(multiple text blocks in the string)
Complete code:
The following is matched:
%a%
%test%
%which%
%will%
%work%
While the following is ignored:
%string%
%should%
%possibly%
I had a string similar to below:
"thisis%a%%test%string%which%should%will%possibly%work%"
and needed to extract the text between the "%"'s, but not between the end "%" and the next starting "%".
Using regular expressions (in powershell), the following pattern will work:
"%[^%]*?(|).*?%"
The "?" after each "*" allows for greediness(multiple text blocks in the string)
Complete code:
[regex]$r = "%[^%]*?(|).*?%" $r.Matches("thisis%a%%test%string%which%should%will%possibly%work%") | %{$_.Value}
The following is matched:
%a%
%test%
%which%
%will%
%work%
While the following is ignored:
%string%
%should%
%possibly%
Subscribe to:
Posts (Atom)