Search

Coding and stuff

Just another WordPress.com site

Tag

Powershell

Powershell stuff

Replaces the command window (to list files type dir or ls or ipconfig). Has full support for .net. These can be written in text files, and run (I think the extension is .ps1).

 

You can start a program…

start notepad

 

TeamCity

…has the option for a powershell build step (rather than msbuild or nant)

 

Access the Registry (HKCurrent user)

cd HKCU:

 

Find running services

get-service

Get-Service | Export-CSV d:\service.csv

Get-Service | Where-Object {$_.status -eq “stopped”} > d:\running-services.txt

 

Open event logs

show-eventlog

Write .net:

# create the FtpWebRequest and configure it

$ftp = [System.Net.FtpWebRequest]::Create(“ftp://ftp.guardianprofessional.co.uk”)

$ftp = [System.Net.FtpWebRequest]$ftp

$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile

$ftp.Credentials = new-object System.Net.NetworkCredential(“username”,”password”)

$ftp.UseBinary = $true

$ftp.UsePassive = $true

# read in the file to upload as a byte array

$content = [System.IO.File]::ReadAllBytes(“C:\me.png”)

$ftp.ContentLength = $content.Length

# get the request stream, and write the bytes into it

$rs = $ftp.GetRequestStream()

$rs.Write($content, 0, $content.Length)

# be sure to clean up after ourselves

$rs.Close()

$rs.Dispose()

 

It has its own IDE

Powershell ISE

Powershell to rename

To rename all files in a [tmpfiles] folder, and replace all spaces with a dash…

 

gci -Path c:\tmpfiles | rename-item -newname {$_.name -replace ” “, “-”}

 

could also be written

gci | %{rni $_ $_.name.Replace(” “, “-”)}

 

The shortcuts here are:

gci = Get-Childitem

% = ForEach-Object

rni = Rename-item

| = pipes into next section

 

To Search for text in a folder

Select-string c:\projects\*\*.aspx -pattern “div”

 

To find big files

Get-ChildItem -path c:mydata -recurse | where  { ($_.Length /1GB) -gt 2 } | foreach { ($_.length/1GB).Tostring(“0.00?) }

 

Filters:

Get-Process | Where-Object {$_.Name –eq “iexplore”}

 

Azure with powershell

http://wappowershell.codeplex.com/

 

Getting Help

help

help gci

get-command

get-command -commandType Application

get-command -commandType Function

Powershell Blogs

http://blogs.msdn.com/b/powershell/

http://technet.microsoft.com/en-us/library/ee177003.aspx

IIS administration

http://blogs.iis.net/sergeia/archive/2009/06/16/how-to-automate-windows-7-powershell-start-up-with-iis-module.aspx

run in elevated permissions:

import-module webadministration

gci iis://sites

gci -Path iis:\

gci iis:\apppools

Get-ItemProperty iis:\\sites\Umbraco49

Get-ItemProperty iis:\\sites\Umbraco49 pspath

set-location

http://technet.microsoft.com/en-us/library/ee909471(v=ws.10).aspx

 

$site = “Stage”

$iispath

Set-ItemProperty $iispath -name physicalpath -value “f:\stage\wwwroot2”

http://pscx.codeplex.com/


Void][Reflection.Assembly]::LoadWithPartialName(“Microsoft.Web.Administration”)

$siteName = “Default Web Site”
$serverName = “Server1”

$serverManager = New-Object Microsoft.Web.Administration.ServerManager
$serverManager = [Microsoft.Web.Administration.ServerManager]::OpenRemote($Server1)
$site = $serverManager.Sites | where { $_.Name -eq $siteName }

$rootApp = $site.Applications | where { $_.Path -eq “/” }
$rootVdir = $rootApp.VirtualDirectories | where { $_.Path -eq “/” }

$rootVdir

Powershell to rename

Another great little feature of powershell is the ability to easily rename a set of files.  For example, to rename all files in a [tmpfiles] folder, and replace all spaces with a dash…

gci -Path c:\tmpfiles | rename-item -newname {$_.name -replace ” “, “-“}

could also be written

gci | %{rni $_ $_.name.Replace(” “, “-“)}

The shortcuts here are:

gci = Get-Childitem

% = ForEach-Object

rni = Rename-item

 

Powershell search file contents

Powershell is a great feature that has been around for a couple of years. Its part of windows, and allows you to do a load of stuff using basic powershell commands, or anything that can be done using .net.

for example to search for some text in a folder you can run:

Select-string c:\projects\*\*.aspx -pattern “div”

Create a free website or blog at WordPress.com.

Up ↑