More Scripts


~ Please see Window Server topic for more scripts ~


on that PC, open up PowerShell and run as Admin:
Join it to a Domain: 
add-computer –domainname softlab4.local -Credential softlab4\admin -restart –force

Remove that PC from a Domain: You have to know the password to the local Admin account if we remove from the domain, or you’ll not able to login after!
remove-computer -credential softlab4\admin -passthru -verbose
restart-computer

-Rename a computer name:
Rename-Computer -ComputerName “win10e” -NewName “win10e5” -DomainCredential DomainAdminUser -Restart


How to start pinging multiple devices at the same time: 
Step 1: 
Copy below into a notepad and Save it as .bat 
——\\\———–
Start
“Checking TCP/IP stack” ping 127.0.0.1 -t
Start “Ping INTERNET” ping http://www.google.com -t
—————–
Step 2: 
Double click on the .bat from above step


It is good idea to keep listing of server PC or Router or Switch. Which you can use batch script to automatics check the status of the devices by ping or by nslookup.



The below vbscript will generate a .bat file script of mapped network drive on the PC.
Step 1: save below script as .vbs
Step 2: run the script, it will create .bat file of mapping drive that you can use to map
to other PC.
——-\\\———

‘ ********** My Code **********
Set wshShell = CreateObject( “WScript.Shell” )

‘ ********** Get ComputerName
strComputer = wshShell.ExpandEnvironmentStrings( “%COMPUTERNAME%” )

‘ ********** Get Domain
sUserDomain = createobject(“wscript.network”).UserDomain

Set Connect = GetObject(“winmgmts://”&strComputer)
Set WshNetwork = WScript.CreateObject(“WScript.Network”)
Set oDrives = WshNetwork.EnumNetworkDrives
Set oPrinters = WshNetwork.EnumPrinterConnections

‘ ********** Current Path
sCurrentPath = CreateObject(“Scripting.FileSystemObject”).GetParentFolderName(WScript.ScriptFullName)

‘ ********** Blank the report message
strMsg = “”

‘ ********** Set objects
Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set objWbem = GetObject(“winmgmts:”)
Set objRegistry = GetObject(“winmgmts://” & strComputer & “/root/default:StdRegProv”)

‘ ********** Get UserName
sUser = CreateObject(“WScript.Network”).UserName

‘ ********** Print user and computer
‘strMsg = strMsg & ” User: ” & sUser & VbCrLf
‘strMsg = strMsg & “Computer: ” & strComputer & VbCrLf & VbCrLf

strMsg = strMsg & “:: COPIED FROM ” & strComputer & ” :: ” & VbCrLf& VbCrLf
strMsg = strMsg & “@echo off” & vbCrLf

For i = 0 to oDrives.Count – 1 Step 2
strMsg = strMsg & “net use ” & oDrives.Item(i) & ” “” ” & oDrives.Item(i+1) & ” “” /user:” & sUserDomain & “\” & sUser & ” /persistent:yes” & VbCrLf
Next
strMsg = strMsg & “:exit” & VbCrLf
strMsg = strMsg & “@pause” & VbCrLf

‘ ********** write the file to disk.
strDirectory = sCurrentPath
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
If objFSO.FolderExists(strDirectory) Then
‘ Procede
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End if

‘ ********** Calculate date serial for filename **********
intMonth = month(now)
if intMonth < 10 then
strThisMonth = “0” & intMonth
else
strThisMonth = intMOnth
end if
intDay = Day(now)
if intDay < 10 then
strThisDay = “0” & intDay
else
strThisDay = intDay
end if
strFilenameDateSerial = year(now) & strThisMonth & strThisDay
sFileName = strDirectory & “\” & strComputer & “_” & sUser & “_MappedDrives” & “_” & strFilenameDateSerial & “.bat”
Set objFile = objFSO.CreateTextFile(sFileName,True)
objFile.Write strMsg & vbCrLf

‘ ********** Ask to view file
strFinish = “End: A .bat was generated. ” & VbCrLf & “Copy the generated file (” & sFileName & “) into the machine where you want to recreate the mappings and double-click it.” & VbCrLf & VbCrLf
MsgBox(strFinish)



Map network share for user automatically:
———–\\\—–
@echo off
::<-comments
:: you can save the files into GPO or
::local below just for that user:
::C:\Users\MrBillGate\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
:: And to get to the link above you can (Window Key + R ) shell:startup

::If the share drive require password.
net use Z: \\win-2019-dc1\myData\foldername password /user:domain\Username /persistent:yes

::no password, you can do the bottom code instead
net use P: “\\win-2019-dc1\myData\foldername”  /persistent:yes

:::




Shutdown PC in 1 second:
Copy below into notepad and then save as .bat
——-\\\———
@echo on
shutdown /s /t 01
pause



Reboot PC in 1 second:
Copy below into notepad and then save as .bat type
———\\\——-
@echo on
shutdown /r /t 01
pause



Ping a device forever:
Copy below into notepad and then save as .bat 
———-\\\——
@echo on
ping 192.168.3.5 -t
pause



DNS lookup and then Ping that devices after: 
Step 1: Create a list of server by PC name, then save as text file type with name: server-list.txt
Step 2: copy below into notepad and then Save File as: DnslookupAndPing.bat 
into the same  folder as step 1 above.
——-\\\——–
@echo off

setlocal enabledelayedexpansion
set OUTPUT_FILE=server-report.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (server-list.txt) do (
set SERVER_ADDRESS=ADDRESS N/A
for /f “tokens=1,2,3” %%x in (‘ping -n 1 %%i ^&^& echo SERVER_IS_UP’) do (
if %%x==Pinging set SERVER_ADDRESS=%%y
if %%x==Reply set SERVER_ADDRESS=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE!
echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
)

pause



DNS lookup by IP: 
Step 1: Create a list of server by IP address , then save as text file type with name: ip.txt
Step 2: copy below into notepad and then Save File as: LookupByIP.bat 
into the same  folder as step 1 above.
——\\\———-
@echo off
for /f %%a in (ip.txt) do call :process %%a
goto :eof
:process
set ip=%1
for /f “tokens=2” %%b in (‘nslookup %ip%^|find /i “Name”‘) do echo %ip% – %%b >> ip2name-report.txt



Joins the current computer to a domain:
copy below into notepad and then save as .bat type
———\\\——-

On Error Resume Next
‘ This script joins the current computer to a domain, using specified user and placing it in specified OU
‘ Created by Sole Viktor – sole@sole.dk

‘ Set these variables
strDomain = “solab3.local” ‘ Domain to logon
strPassword = “mypassword” ‘ Service account logon password
strUser = “Administrator” ‘ Service account
strOU = “OU=BellFlowerPC,DC=softlab3,DC=local” ‘ OU to place computer in

‘ Constants to choose from when joining
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144

Set objNetwork = CreateObject(“WScript.Network”)
strComputer = objNetwork.ComputerName

‘ Join Domain
Set objComputer = GetObject(“winmgmts:{impersonationLevel=Impersonate}!\\” & _
strComputer & “\root\cimv2:Win32_ComputerSystem.Name='” & _
strComputer & “‘”)
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
strPassword, strDomain & “\” & strUser, strOU, _
JOIN_DOMAIN + ACCT_CREATE + DOMAIN_JOIN_IF_JOINED)

Select Case ReturnValue

Case 0 Status = “Success”

Case 2 Status = “Missing OU”

Case 5 Status = “Access denied”

Case 53 Status = “Network path not found”

Case 87 Status = “Parameter incorrect”

Case 1326 Status = “Logon failure, user or pass”

Case 1355 Status = “Domain can not be contacted”

Case 1909 Status = “User account locked out”

Case 2224 Status = “Computer Account already exists”

Case 2691 Status = “Already joined”

Case Else Status = “UNKNOWN ERROR ” & ReturnValue

‘ Show Status
WScript.Echo “Join domain status: ” & Status

End Select


Published by

Khmer Certified

Welcome to Khmer Certified. We're all about sharing the experienced in Information Technology. Our site is not an introduction to computer. We are more about sharing your journey that may take to get a job in an entry level IT - your first year in IT field

Leave a comment