Quick Powershell Tutorial
Not a lot of help text but once you try these commands, you'd learn 90% of the commands most people need from powershell.
Powershell Lab
WMI Object
gwmi win32_service
gwmi -list
gwmi -list | sort-object
gwmi -list -computer switools-vm | sort-object
Quick Detour
gwmi -query "select * from win32_service where name like 'a%'"
gwmi win32_service | where-object {$_.name -like "a*"}
get-process | where-object {$_.name -like "b*"}
Get-EventLog System | Group-Object eventid | Sort-Object Count -descending
Get-EventLog System | Group-Object eventid | Sort-Object Count -descending<enter> | ConvertTo-html | out-file "c:\test\test.html"
Back to WMI
gwmi -list -namespace "root\virtualization" -computer switools-vm | sort-object
(gwmi -computer switools-vm -NameSpace "root\virtualization" -Class "MsVM_VirtualSystemManagementServiceSettingData") | get-member
(gwmi -computer switools-vm -NameSpace "root\virtualization" -Class "MsVM_VirtualSystemManagementServiceSettingData").DefaultVirtualHardDiskPath
Powershell Versus C#
Powershell
$ImgMgtSvc=Get-WmiObject -computerName $server -NameSpace "root\virtualization" -Class "MsVM_ImageManagementService"
$arguments = @($vhdPath,$parentDisk,$null)
$result= $ImgMgtSvc.PSbase.InvokeMethod("CreateDifferencingVirtualHardDisk",$arguments)
C#
ConnectionOptions oConn = new ConnectionOptions();
oConn.Username = "JohnDoe";
oConn.Password = "JohnsPass";
ManagementScope scope = new ManagementScope(@"\\" + server + @"\root\virtualization", oConn);
ManagementObject imageService = Utility.GetServiceObject(scope, "Msvm_ImageManagementService");
ManagementBaseObject inParams = imageService.GetMethodParameters("CreateDifferencingVirtualHardDisk");
inParams["ParentPath"] = parentPath;
inParams["Path"] = path;
ManagementBaseObject outParams = imageService.InvokeMethod("CreateDifferencingVirtualHardDisk", inParams, null);
inParams.Dispose();
outParams.Dispose();
imageService.Dispose();
Functions and Filters
Function Add($x, $y)
{
$x + $y
}
add 5 4
Function FnFilter($name)
{
$input | where-object {$_.name -like "*$name*"}
}
get-process | FnFilter a
for fun: Get-ChildItem -Path C:\ -Recurse | FnFilter temp
Profile
$profile
notepad $profile
Credentials
$cred = get-credential
$cred
$cred|get-member
$cred.password
$cred.password |convertfrom-securestring | set-content c:\temp.txt
$password = Get-Content c:\temp.txt | ConvertTo-SecureString
$credential = new-object system.management.automation.pscredential("redmond\mengli", $password)
.NET
[system.environment] | get-member -static
[system.environment]::GetCommandLineArgs()
[system.environment]::CurrentDirectory
[appdomain]::currentdomain.getassemblies()
notepad++ mymathlib.cs
C:\Windows\Microsoft.NET\Framework\v3.5\csc /target:library mymathlib.cs
mymathlib.cs
namespace MyMathLib
{
public class Methods
{
public Methods()
{
}
public static int Sum(int a, int b)
{
return a + b;
}
public int Product(int a, int b)
{
return a * b;
}
}
}
[Reflection.Assembly]::LoadFile("c:\test\MyMathLib.dll")
[appdomain]::currentdomain.getassemblies()
[MyMathLib.Methods]::Sum(10, 2)
(new-object MyMathLib.Methods).product(10,2)
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[system.windows.forms.messagebox]::show("bleh")
COM
$comobj = new-object -comobject axfuzz.simple
$comobj.simple(1)
Further Applications
TFS Library from the TFS Power Tools, ActiveDirectory