Posts

Showing posts with the label Power Shell Scripting

PowerShell - Managing Remote sessions

Image
Enabling PowerShell Remoting On the computer we want to access remotely and execute PowerShell command remotely, we need to run following command: Enable-PSRemoting -Force   This command starts the WinRM service, sets it to start automatically with your system, and creates a firewall rule that allows incoming connections. The -Force part of the command tells PowerShell to perform these actions without prompting you for each step.   Executing PowerShell script  Invoke-command –filepath “path” – computername “computername” For E.g. invoke-command -filepath C:\Bin\ADScripts\PKI\retrieveExpiringCertificates.ps1 -computername HQCAS02 Reference: http://www.tomsitpro.com/articles/powershell-remote-management,2-781.html http://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/

Power Shell stuff

1.  Replacing characters in csv file using below sample code: $csv001 = import-csv "C:\Users\sumeet.kumar\Desktop\Services\Scripts\Power Shell Scripts\Sending reboot notification\Computers with last logged on user from collection Username0.csv" $csv001 | foreach-object { $_.username0 = $_.username0.replace("Domain\","") } $csv001 | export-csv  "C:\Users\sumeet.kumar\Desktop\Services\Scripts\Power Shell Scripts\Sending reboot notification\Computers with last logged on user from collection Username001.csv" -notype Reference: http://social.technet.microsoft.com/Forums/windowsserver/en-US/0ad9ad37-b99b-418a-b139-34752b0927dc/powershell-importcsv-then-replace-character-then-exportcsv 2.  Appending characters in csv file using below code: $csv003 = import-csv "C:\Users\sumeet.kumar\Desktop\Services\Scripts\Power Shell Scripts\Sending reboot notification\Computers with last logged on user from collection Username001.csv" $csv003 | fo...

Remove Empty lines from text file

Sample Code: get-content "C:\users\sumeet.kumar\Desktop\Services\Scripts\Power Shell Scripts\List All Secondry Drive\sumeet1.txt" | Foreach-Object { ($_  -replace '\s','') } > "C:\Users\sumeet.kumar\Desktop\Services\Scripts\Power Shell Scripts\List All Secondry Drive\FileWithNoEmptyLines.txt"

How to Remove First two lines of text file or csv

How to Remove First two lines of text file Sample Code for text file: (get-Content "C:\Users\sumeet.kumar\Desktop\Services\Scripts\Power Shell Scripts\List All Secondry Drive\Machinelist.txt" | Select-Object -Skip 4) | out-file "C:\Users\sumeet.kumar\Desktop\Services\Scripts\Power Shell Scripts\List All Secondry Drive\sumeet.txt" Sample Code for .csv file: $csv = Get-Content "C:\Users\sumeet.kumar\Desktop\Services\Scripts\Power Shell Scripts\List All Secondry Drive\Machinelist.txt" $csv = $csv[2..($csv.count - 2)] $csv > "C:\Users\sumeet.kumar\Desktop\Services\Scripts\Power Shell Scripts\List All Secondry Drive\machinelist1.txt"

Get-WMIObject Service finding unquoted path

Get-WmiObject -Class Win32_Service -ComputerName INSKUMAR02 | select-object PSComputerName,Name,PathName,startMode  | where-object {$_.startMode -eq "Auto" -and $($_.PathName).Substring(0,1) -ne '"' -and $($_.PathName).Substring(0,$($_.PathName).LastIndexOf("\") + 1).Contains(' ') }

Powershell Script to check if any server is pending for reboot

After the security patch, we need to validate if any of our critical server is pending for reboot and take necessary actions. PowerShell script and use it to identify servers that are pending for reboot.This will avoid the scenarios of unexpected server reboot.   # Filename:  CheckForPendingReboot.ps1 # Description: Imports a list of computers from a text file and then checks each of the #    computers for the RebootPending registry key. If the key is present, #    the script restarts the computer upon confirmation from the user. # Assumes the existence of a file called 'Servers.txt' in the same directory, populated with # the NETBIOS names of a list of computers, each on a new line.     $computernames = gc Servers.txt  foreach ($computername in $computernames)  {  $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$computername)  $key = $baseKey.OpenSubKey("Software\Mic...

Exporting attributes from Active Direcotory

Import-module ActiveDirectory Get-ADUser -Filter * -SearchBase "OU=Others,OU=traveling,OU=Employees,dc=Aryan,DC=net" -Properties givenname,sn,sAMAccountName,mail,mobile,manager,telephonenumber,title | Select givenname,sn,sAMAccountName,mail,mobile,manager,telephonenumber,title | Export-CSV "C:\Scripts\Exporting_Users_From_AD\Travelling_Others.csv"

Parsing CSVs in Power Shell

I  was messing around with some log files for the  podcast  and exported a CSV. There's lots of ways for us to get stats, but I was just poking around. Given a CSV format like this: "File","Hits","Bandwidth" "/hanselminutes_0026_lo.wma","78173","163625808" "/hanselminutes_0076_robert_pickering.wma","24626","-1789110063" "/hanselminutes_0077.wma","17204","1959963618" "/hanselminutes_0076_robert_pickering.mp3","15796","-55874279" "/hanselminutes_0078.wma","14832","-1241370004" "/hanselminutes_0075.mp3","13685","-1840937989" "/hanselminutes_0075.wma","12129","1276597408" "/hanselminutes_0078.mp3","11058","-1186433073" And noticing that there are different file extensions, but one logical show...for example ther...

Power Shell to Create DFS-R Diagnostic Reports.

Script to generate a new DFS- HTML Health Report and Propagation report for the "Replication Group" using the native dfsradmin.exe tool. http://www.microsoft.com/en-in/download/details.aspx?id=11470 Sample Script $ReportFilePath = "C:\DFSReports\Propagation-sx.net_vol1_marketing-"+(Get-Date -UFormat %Y-%m-%d) $ReferralDC = "sx\sbmx01-neerav.sx.net" #$reportFilePath1 = "C:\Marketing" $SmtpServer = "sbmx01-neerav.sx.net" $EmailFrom = " sumeet.kumar@sx.net " $EmailTo = " testuser3@sx.net " #Run Dfsradmin.exe to generate Health  Report DfsrAdmin.exe Health New /RgName:`"sx.net\vol1\marketing`" /RefMemName:$ReferralDC /RepName:$ReportFilePath /FsCount:true #Run Dfsradmin.exe to generate to generate Propagation test and Report DfsrAdmin.exe Proptest New /RgName:`"sx.net\vol1\marketing`" /RfName:"Marketing" /MemName:"sx\sbmx01-neerav" /domain:"sx.net...

Remotely find a Server’s Manufacturer and Serial Number

strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colBIOS = objWMIService.ExecQuery _ ("Select * from Win32_BIOS") For each objBIOS in colBIOS Wscript.Echo "Manufacturer: " & objBIOS.Manufacturer Wscript.Echo "Serial Number: " & objBIOS.SerialNumber Next

Remotely execute cmd.exe commands on multiple computers

If it works in cmd.exe , you can remotely execute on multiple Computers . This script invokes whatever command you can use in cmd.exe on one or more computers you input the command you'd like to run as a screen input when you run the script, you can use all cmd.exe command like [del,ipconfig /flushdns,ipconfig /registerdns,gpupdate /force ,notepad.exe,defrag c:, wuauclt /detectnow , powercfg, net start ,net stop,copy,arp,wscript.exe ....] #============================================================================================== # Script Name : Run Remote cmd.exe Commands # COMMENT: # this script invokes whatever command you can use in cmd.exe on one or more computers #you input the command name as a screen input when you run the script #you can use all cmd.exe command like [del,ipconfig /flushdns,ipconfig /registerdns,gpupdate /force ,notepad.exe,defrag c:, ... #..wuauclt /detectnow,powercfg,net start ,net stop,copy,arp,wscript.ex...

Active Directory New user creation Tool

Active Directory New user creation Tool: Features: Allows user creation with oft-used Active Directory attributes Bulk creation of users from CSV Auto-generation of account attributes based on other attributes Display Name samAccountName userPrincipalName Default entries Domain OU Phone Number (can use full number or company prefix '212-555-') Department Company Description Password (Accounts are set to change at first logon) Site (HQ, Branch Office 1, etc) Street Address City State Postal Code Pre-populated fields for easy selection Address information Domains OUs Descriptions Departments Links: http://blog.richprescott.com/2011/10/arposh-new-user-creation-anuc.html http://gallery.technet.microsoft.com/scriptcenter/New-User-Creation-tool-14fa73cd

Client System Administration tool

Client System Administration Tool: Features: Connects to currently logged-in domain on startup Search for PC - Search Active Directory for computers matching the string in the textbox System Info - Gathers info about PC, user session, make/model, hardware, OS, networking and McAfee Local Admins - Enumerates local administrators and allows you to remove them Applications - Enumerates installed software and allows you to uninstall Startup Items - Enumerates startup items and allows you to remove them Processes - Enumerates running processes and allows you to kill them Remote Desktop - Remote desktop into computer Remote Assistance - Initiate remote assistance session with remote computer View C Drive - Opens explorer to the C drive of remote computer Restart Computer - Restarts remote computer (Includes confirmation) File Menu Connect to domain... - Connect to a different domain Find User in AD - Enter user's name, search for it in AD and output to Gr...

Search AD and Add Employee ID–PowerShell Script

1. http://portal.sivarajan.com/2011/07/add-employee-idpowershell-script.html

10 reasons why you should learn to use PowerShell

PowerShell is a powerful scripting tool that can greatly expedite your admin tasks. If you haven’t had a chance to learn how to use it, you might want to make time for it now. Here are some reasons why the effort will pay off. 1: It’s not going away any time soon Microsoft has made it clear that Power Shell is here to stay. In fact, PowerShell version 2 is not only included in Windows Server 2008 R2 and in Windows 7, it is enabled by default. Part of the reason why Microsoft has done this is that going forward, many add-on products will be based on PowerShell. 2: Most Microsoft products will eventually use it Virtually all of the server products Microsoft is producing right now can be managed through PowerShell. From an administrative standpoint, this means that if you become proficient in PowerShell, you will have the skill set necessary for managing most of Microsoft’s newer products. The basic built-in PowerShell commands are used in every product that supports Pow...

Searching Active Directory with Windows PowerShell

1. http://technet.microsoft.com/en-us/library/ff730967.aspx 2. http://www.powershellpro.com/powershell-tutorial-introduction/powershell-tutorial-active-directory/

How to: Refer to Cells and ranges

http://msdn.microsoft.com/en-us/library/bb211395(v=office.12).aspx

Power Shell Script Repository

1. http://gallery.technet.microsoft.com/scriptcenter 2. http://www.myitforum.com/myITWiki/WPScripts.ashx 3. http://portal.sivarajan.com/search?q=script+powershell&max-results=20