Showing posts with label Tips. Show all posts
Showing posts with label Tips. Show all posts

Thursday, September 5, 2013

Powershell Techniques : Use While loop for rebooting laptop after 30 seconds with a informative message .




Hi,

I hope you all are doing well, I am in Italy for this week and enjoying Italian coffee.

Anyway.

In few of my scripts, before rebooting the laptop, I use Start-Sleep cmdlet to give a pause for 30-60 seconds.

We can do this easily using Start-Sleep 30.But, it is always good to show informative message on the screen so that user can know what is going on, to do that, I use while loop.

See the code below and it is very simple.


#variable
$second = 30

#While Loop

#while our $second variable is greater then 0
While ($second -gt 0 ) {

            # we are minus 1 with every look
            $second--
           
            # putting laptop sleeping for one seconds
            sleep 1
           
            # just a message
            Write-Warning "Computer will be restated in  $second"
           
            }

Logically this loop will run 30 times, until $seconds value is not come to zero.

Let's run the code and the result is. nice informative message about that laptops will be reboot in 30 seconds.




















Thanks

I hope you may like this. :) 


Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005clip_image007







Sunday, November 20, 2011

Turning SIDs into Real Names using PowerShell

 

PowerTip of the Day, from PowerShell.com:

Sometimes, you'd like to turn security identifiers (SIDs) into real names. Here is a function that can do this for you:

   1: function SID2Name($sid){
   2:  
   3:   $objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)
   4:  
   5:   try {
   6:  
   7:   $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
   8:  
   9:   $objUser.Value
  10:  
  11:   } catch { $sid }
  12:  
  13: }

And, here is a show case for the function: to enumerate all profiles on your computer, you can read them from the Registry. However, all profiles are stored with SIDs only. Thanks to your new function, you can now display the real user names of everyone who has a profile on your machine:



   1: function Get-Profile {
   2:  
   3: $key = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
   4:  
   5: dir $key -Name | ForEach-Object { SID2Name $_ }
   6:  
   7: }


Thanks to www.Powershell.com