How to determine Powershell version and run an older Powershell Version

To get the powershell version run:

$psversiontable

To run another version of powershell run for example:
powershell -version 2.0

No Comments »

admin on July 24th 2013 in IT, PowerShell

Install SNMP with Powershell on Windows 2012

If you run the command without the management tools then you won’t have the tabs to configure the snmp-service. By the way the argument –Whatif shows you in advance what would be installed if you run the command. So to actually installe it run the command without –WhatIf.

#Add-WindowsFeature -Name SNMP-Service -IncludeAllSubFeature -IncludeManagementTools –Restart –WhatIf

No Comments »

admin on June 4th 2013 in IT, PowerShell, Windows Server

Enable Settings to be able to manage a Windows Server 2012 Core

Powershell
Enable-PSRemoting –Force

Tests
Test-WSMan –ComputerName [Computer]
Enter-PSSession –ComputerName [Computer]

Manage via Server Manager
Configure-SMRemoting.exe –Enable

MMC
Enable-NetFirewallRule –DisplayGroup “Windows Remote Management”

Remote Desktop
cscript C:\Windows\System32\Scregedit.wsf /ar 0

or just conifgure it via sconfig

Tests
Run from another machine mstsc and connect to the server.

No Comments »

admin on May 2nd 2013 in Windows Server

Remote Session Powershell

Like ssh you can run a remote session in powershell.

Test if remote session is enabled
#Test-WsMan [Computername]

If you get a result, it should be up and running. Otherwise you need to activate it on the remote server like this.

Enable PowerShell Remoting on a Server
1. Run Powershell as an administrator
2. #Enable-PSRemoting –Force
This command starts the WinRM server service and sets it automatically to start with the system. It also creates a firewall rule that allows incoming connections.

Workgroup
If your computers aren’t in a domain you need to do a couple of more steps to make a trust between them. You need to edit the TrustedHosts on both sides. You could put the IP Adresses comma-seperated instead of * to further restrict it. With the * it will allow any host.

1. #Set-Item wsman:\localhost\client\trustedhosts *
2. #Restart-Service WinRM

Execute a Remote Command
#Invoke-Command -ComputerName COMPUTER -ScriptBlock { COMMAND } -credential USERNAME

Start a Remote Session
#Enter-PSSession -ComputerName COMPUTER -Credential USERNAME

No Comments »

admin on February 8th 2013 in PowerShell

Search for files and copy the result somewhere

Get-ChildItem -r *lana*.mp3 | Copy-Item -destination D:\Lana

No Comments »

admin on February 1st 2013 in PowerShell

Recursive Search Powershell

Short example:
dir –R –I *.exe

Long one:
Get-ChildItem -recurse –include *.exe

Search inclusive text:Get-ChildItem -recurse *.exe | Select-String searchtext

No Comments »

admin on November 27th 2012 in IT, PowerShell

Powershell

Powershell is awesome.

Here a collection of some snippets:

Check who is accessing a file on a server:
net file | where {$_ –match “$filename”}

More:
Get-Service | Sort-Object -Descending Status | ForEach{if($_.Status
-eq “Running”){Write-Host $_.Name -ForegroundColor green} else {Write-Host $_.Na
me -ForegroundColor red}}

Get-Service | ConvertTo-Html -Property Name, Status | ForEach {if($_
-like “*<td>Running</td>*”) {$_ -replace “<tr>”, “<tr bgcolor=green>”} else {$_
-replace “<tr>”, “<tr bgcolor=red>”}} > .\get-service.html

Get-PSDrive | Sort-Object Used -Descendind

New-PSDrive -name FK -psprovider FileSystem -root C:\Temp\Powershell\Files

Set-Location FK:
Get-ChildItem * -Exclude *.tmp, *.temp | Select-Object name, length | Sort-Object Length, Name -Descending

Get-Alias | Sort-Object

Get-Service | Group-Object Status
Get-Service | Get-Member
Get-Service | where{$_.status -eq “stopped”}

Get-ChildItem | Group-Object Extension | Sort-Object Count -Descending
Get-ChildItem | Measure-Object length -average -sum -maximum -minimum
Get-ChildItem | where-object{$_.length -gt 2MB}
Get-ChildItem | where {$_.PSIsContainer}

$a=Get-ChildItem | Select-Object Extension | Sort-Object Extension -unique
$a | ForEach {“.\Neu”+$_.Extension} | ForEach {NewItem $_ -type directory}

$a=Get-ChildItem | where {$_.PsIsContainer -eq -0}
$a | foreach{Copy-Item $_.FullName (“.\Neu” + $_.Extension + “\” + $_.Name)}

Get-ChildItem | foreach{$_.IsReadOnly = -1}

Get-ADUser -Filter *
Get-Command *user*

No Comments »

admin on May 16th 2012 in PowerShell

How to output PowerShell Command into a Textfile

Simply add this to your command:
| out-File Path$

No Comments »

admin on July 8th 2011 in IT, PowerShell

Create a user with Windows PowerShell

Windows PowerShell is a new feature which comes with Windows 2008 server. It need to have the .Net Framework installed and can also be installed on Windows 2003 Server, Windows XP-Vista.

Create a textfile “NewUser.ps1” with following content:
$objOU=[ADSI]LDAP://OU=Users,DC=test,DC=com

$objUser = $objOU.Create("user", "CN=Test1 User")
$objUser.Put("sAMAccountName", "Test1.User")
$objUser.SetInfo()

$objUser = $objOU.Create("user", "CN=Test2 User")
$objUser.Put("sAMAccountName", "Test2.User")
$objUser.SetInfo()

Important: To run this scripts in PowerShell you need to first active it:
set-executionpolicy remotesigned

Go into the directory where the script is located and run it like this:
./NewUser.ps1

Get users samid:
dsquery user -name "*User" | dsget user -samid

No Comments »

admin on June 5th 2009 in Windows Server




Check out new movies online website. Download and buy movies now.
1