Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Tuesday, September 29, 2020

Create local user using powershell script

 First of all, prepare the user list as below and save it as users.csv file


Create a powershell script with the following code


The scripts will first need to convert the password string from the csv file into a secureString. This is necessary for New-LocalUser cmdlet. After that, the script will set PasswordNeverExpires option as well as add those user into Administrators group.

I am using this script for my test setup which need a lot of dummy users. For production environment, you can actually create random password instead of specifying password in the users.csv file and set the "-ChangePasswordAtLogon $true" option. And obviously, you can also change the User group to "Users" group instead of "Administrators" group.

Thursday, June 28, 2012

PowerShell to check MSSQL database status

A very simple Powershell script to check database status.

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') ServerName
$db = $s.Databases.item("databaseName")
"status: " + $db.status + " isaccessible: " + $db.isaccessible

Example output:
Database status is normal
status: Normal isaccessible: True

This output is created by renaming the log file.
status: RecoveryPending isaccessible: False

For more status description, please refer to this Microsoft site.