Posts

Showing posts with the label Scripting

View PowerShell commands executed history

Look at below files at the below location to view the PowerShell commands executed history: ConsoleHost_history.txt Visual Studio Code Host_history.txt C:\Users\<UserName>\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine

PowerShell Script to get all registry keys and values

Below one liner may help to get all registry keys and their values under given path: Get-Item ' ' | %{Get-ItemProperty -Path $_.PSPath} Example registry path :  HKLM:\SOFTWARE\7-Zip Get-Item ' HKLM:\SOFTWARE\7-Zip'   | %{Get-ItemProperty -Path $_.PSPath} Output: PS C:\> Get-Item 'HKLM:\SOFTWARE\7-Zip' | %{Get-ItemProperty -Path $_.PSPath} Path         : C:\Program Files\7-Zip\ Path64       : C:\Program Files\7-Zip\ PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE PSChildName  : 7-Zip PSProvider   : Microsoft.PowerShell.Core\Registry

Get PowerShell version in a Windows system

Ways to get PowerShell version in a Windows system $PSVersionTable.PSVersion (Available in PS version 2 or higher) get-host (For PS version 1) powershell -Command "$PSVersionTable.PSVersion" powershell -command "(Get-Variable PSVersionTable -ValueOnly).PSVersion " Outputs: $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 3 0 -1 -1 get-host Name : ConsoleHost Version : 3.0 InstanceId : 9b7e8224-e1d5-4b22-b0cb-53f19cd9c5b8 UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : en-US CurrentUICulture : en-US PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace

PowerShell command to get last modified files

PowerShell command to get last modified files: Dir " " -r | ? {! $_.PSIsContainer} | sort LastWriteTime | select LastWriteTime, FullName -last 20 | sort LastWriteTime –Descending | ft -autosize Example: Dir " c:\temp " -r | ? {! $_.PSIsContainer} | sort LastWriteTime | select LastWriteTime, FullName -last 20 | sort LastWriteTime –Descending | ft -autosize

Ways to execute PowerShell (PS1) scripts in remote computers

Below are ways to execute PowerShell (PS1) scripts in remote computers: Example 1: invoke-command -computerName MySrv1 -filepath .\test.ps1 Example 2: Get the version of the PowerShell host running on a remote computer: invoke-command -computername server64 -scriptblock {(get-host).version} Example 3: Get the version of the PowerShell host running on a list of remote computers (computers.txt): PS C:\> $version = invoke-command -computername (get-content computers.txt) -scriptblock {(get-host).version} PS C:\> $version Example 4: Run the Sample.ps1 script on all of the computers listed in the Servers.txt file. Using the -FilePath parameter to specify the script file has the effect that the content of the script is automatically copied into a script block and then passed to and run on each of the remote computers: PS C:\> invoke-command -comp (get-content servers.txt) -filepath c:\scripts\sample.ps1 -argumentlist Process, Service Alternatives method: Inv...

Script to get disk space in MB (including MountPoints).

I'm looking for a simple script to get disk space in MB (including MountPoints). Below is the latest vbs script I'm going to try: strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk where drivetype <> 5") WScript.Echo "Name Capacity Free Free %" For each objDisk in colDisks percentfree = (objdisk.Freespace / objdisk.Size) * 100 With objDisk WScript.Echo .DeviceId & " " & pad(FormatNumber(.Size/1048576, 2)) & " " & pad(FormatNumber(.Freespace/1048576, 2)) & " " & FormatNumber(percentfree, 2) End With Next function pad(strText) strText = Space(12) + strText strText = Right(strText, 10) pad = strText end function Ref: http://www.computerhope.com/forum/inde...

Script to get disk space in MB (including MountPoints).

I'm looking for a simple script to get disk space in MB (including MountPoints). Below is the latest vbs script I'm going to try: strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk where drivetype <> 5") WScript.Echo "Name Capacity Free Free %" For each objDisk in colDisks percentfree = (objdisk.Freespace / objdisk.Size) * 100 With objDisk WScript.Echo .DeviceId & " " & pad(FormatNumber(.Size/1048576, 2)) & " " & pad(FormatNumber(.Freespace/1048576, 2)) & " " & FormatNumber(percentfree, 2) End With Next function pad(strText) strText = Space(12) + strText strText = Right(strText, 10) pad = strText end function Ref: http://www.computerhope.com/forum/inde...

Using icacls.exe command line utility to set NTFS permissions on folders and files

Below is the syntax for: Simple example: icacls C:\Windows /grant Administrator :F Will grant the user Administrator Full permission to C:\Windows folder Advanced example: icacls C:\Windows /grant Administrator :(OI) (CI)F /T /C Will grant the user Administrator Full permission to C:\Windows folder and subfolders and files, operation will continue on all file errors and set inheritance flag as Object interitance, Container inheritance inheritance rights may precede either form and are applied only to directories: (OI) - object inherit (CI) - container inherit (IO) - inherit only (NP) - don't propagate inherit /T indicates that this operation is performed on all matching files/directories below the directories specified in the name. /C indicates that this operation will continue on all file errors. Error messages will still be displayed.

Changing the default scripting engine from Wscript to Cscript

Change the default scripting engine from Wscript to Cscript. (The Xcacls.vbs script works best in Cscript.) To do this, type the following at a command prompt, and then press ENTER: cscript.exe /h:cscript Note Changing the default scripting engine to Cscript only affects how scripts write to the screen. Wscript writes each line individually to an OK dialog box. Cscript writes each line to the command window.

Soft reset Dell DRAC through SSH

Open Putty.exe Enter Hostname or IP address, port number leave as default (22). Open You will be connected to CLI of DRAC Issue the below command: racadm racreset Above command will perform soft reset of DRAC.

Logon scripts FAQ

Frequently Asked Questions About Logon Scripts:   1.How do I setup Logon scripts in a domain with Active Directory?   2.Why would I choose one method over another?   3.Can I use both methods to assign Logon scripts?   4.How do I setup Logon scripts so they support all of my clients?   5.How do I configure a Logon script for a user on the "Profile" tab in AD Users & Computers?   6.What languages can I use for Logon scripts?   7.Can I use a VBScript program for a Logon script on all clients in my domain?   8.How do I configure a Logon script with Group Policy?   9.What about Logoff, Startup, and Shutdown scripts in Group Policy?   10.What permissions are required for Logon scripts to run?   11.What can be done with a batch file Logon script, besides launch a VBScript program?   12.What about Logon scripts in an NT domain? Answers:  1. How do I setup Logon script...

Shortcut for Elevated Command Prompt (Windows 2008,7,Vista)

Here is the easiest way to open elevated command prompt: Click on the "Start" button In the "Start Search" box type the name of the program you want to run as administrator. For example type CMD or CMD.EXE. Do NOT hit ENTER. Instead hit CTRL+SHIFT+ENTER. It seems that this key combo triggers user account elevation and is identical to the RUNAS command or the GUI "Run as administrator" option. You will be prompted by the UAC warning window. Click on CTRL+C or click on the "Continue" button. Note that the CMD window has an "Administrator:" appended to the window title. You can now perform any administrative tasks you want. Reference:http://www.petri.co.il/quickly_open_cmd_with_elevated_credentials_in_windows_vista.htm