Category: Tools


Get system uptime


We all have our own ways of finding a servers / computers uptime.
But I thought I’d share my favorite way:

 

function Get-SrvUptime
{
$operatingSystem = Get-WmiObject Win32_OperatingSystem
[Management.ManagementDateTimeConverter]::ToDateTime($operatingSystem.LastBootUpTime)
}


Restarting a service with Task Scheduler


Some times you need to scheduler a restart of services on servers or computers, and I used to do this via a batch job, that I called from Task Scheduler, but..

I found a much easier way of doing this,  just create a new task, and add the Actions like bellow.

NET as the program, and START/STOP “SERVICENAME” as the argument

and voila, there it is 🙂


Create #HASHED password file for PowerShell use


If you want to automate some Powershell scripts to do a job for you, and you don’t want to (and you never should) add the password in the script, then this is a great ting.

You create an encrypted txt file based on the userID and PW you define in the prompt, the file is then created with the password (password only) information in the encrypted file. The export location must be the location you want the script to run, as you can not move/copy the file to a different location after export.
So now you can use the password file with the scripts you have created
#==============================================================================================
# NAME: Encrypt Password for use in Powershell
# AUTHOR: Vincent Christiansen, vincent@sameie.com
# DATE  : 21/01/2016
# COMMENT: Will prompt you for username and password, and will encrypt (to hash) the password to a txt file.
#          This will only be the password. And you must dump the file to the location where you are going to 
#          get it from in the other script
# ==============================================================================================
$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content D:\Scripts\Azure_Encrypted_Password.txt

Connect to Azure/Office365 based on encrypted txt file


This will create a remote PowerShell session to your Azure/Office365 based on the Username you specify and the #hashed password text file you created in the previous​ post (that you can find here)
#==============================================================================================
# NAME: Connect to Azure/Office365
# AUTHOR: Vincent Christiansen, vincent@sameie.com
# DATE  : 21/01/2016
# COMMENT: This script will create a remote session in Azure/Office365 based on the encrypted file you have created.
#
# ​​=============================================================================================
$username = “YourServiceAccount@YOURDOMAIN.onmicrosoft.com”
$encrypted = Get-Content “D:\Scripts\Azure_Encrypted_Password.txt” | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PsCredential($username,$encrypted)
Import-Module MSOnline
Connect-MsolService -Credential $cred
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/?proxymethod=rps -Credential $cred -Authentication Basic -AllowRedirection
$importresults = Import-PSSession $ExchangeSession

Get MAC address from remote computer


In some settings you need to get a remote computers MAC addresses. And you don’t have access to it physically.

  1. ​Open a CMD window with you Administrative user (one that has admin access to computer objects)
  2. Ping the computer name (to get IP)
    f.eks: PING COMPUTERNAME
    Wait for reply.. 192.168.25.25
  3. type inn getmac /s 192.168.25.25 /v ​

Now you get a list with the MAC addresses.
getmac-address.PNG

Select Azure Subscription to work with


When you have a larger organisation, you will have several Azure Subscriptions to work against. So here is how you switch.

1. Open Powershell

2. Type in: Add-AzureRmAccount​
(in pop-up enter admin id and password)

3. It will list up the subscription you are connected to when you have authenticated


4. Type in: Get-AzureRmSubscription
(will list out all your subscriptions)

5. Find the Subscription you want to connect to

6. Type in: Select-AzureRmSubscription -SubscriptionId “ENTER THE SUBSCRIPTION ID”

You can choose SubscriptionName too, but I prefere SubscriptionID


Count directory Objects in Active Directory


Some times you need to find out how many directory Objects you have in your AD.
A quick way of getting this done is to use the following PowerShell​ string

​​Get-ADObject -Filter {name -like ‘*’} -SearchBase ‘CN=Schema,CN=Configuration,DC=sameie,DC=COM’ -ResultSetSize $null | Measure-Object >c:\tmp\object_dump.txt

This will dump the information into a easy to read text file

object_count_output.PNG

Now you know how many Objects you have.


No more download 1 GB Test files


Tired of looking on the web for a big test file, or creating your own locally?
Then this is perfect for you too 🙂
Open CMD in administrator mode
Run the following command:
fsutil file createnew <file> <size in bytes>
F.eks this will create a 10 GB file in my tmp folder:
fsutil file createnew c:\tmp\10gb.test 10737418240
The key is to input the size of the file in bytes so here are some common file sizes to save you from math:
1 MB = 1048576 bytes
100 MB = 104857600 bytes
1 GB = 1073741824 bytes
10 GB = 10737418240 bytes
100 GB =107374182400 bytes
1 TB = 1099511627776 bytes
10 TB =10995116277760 bytes