Archive for the ‘ Programming ’ Category

Backup / Restore startmenu design (Layout)

If you want to backup your startmenu layout you can export using:

1
export-startlayout –path C:\layout.xml

BUT,You can import a Start Menu layout using Group Policy Editor (gpedit). After importing the layout it will be fixed, that is you will not be able to change that layout by moving the tiles around.

or better yet copy it as below.

 

backup:

Files are saved in the location of the script.

1
2
Reg export ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\CloudStore\Store\Cache\DefaultAccount") ("backup_startmenu.reg") /y
Copy-Item ("$env:LOCALAPPDATA\Microsoft\Windows\Shell\DefaultLayouts.xml") -Destination ("backup_startmenu.xml")

 

restore:

1
2
3
Reg delete ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\CloudStore\Store\Cache\DefaultAccount") /f
Reg import ("backup_startmenu.reg")
Copy-Item ("backup_startmenu.xml") -Destination ("$env:LOCALAPPDATA\Microsoft\Windows\Shell\DefaultLayouts.xml")

 

All in one menu and options (save as anything.ps1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#------------------------------------------------------------------------------
# AUTHOR(s):
# Kyle Wadman
# 12/2019
# -----------------------------------------------------------------------------
CLS
$Loop = $true
While ($Loop)
{
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Start | PowerShell Script menu
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CLS
write-host
write-host +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
write-host "Startmenu backup/restore | -V1.0 Windows 10"
write-host +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
write-host
write-host -ForegroundColor yellow "Your can use the export-startlayout command to complete this task but on importing back into the system,"
write-host -ForegroundColor yellow "your startmenu will be fixed to the exported design. This method doesn't restricted changes after."
write-host
write-host 'PLEASE SELECT AN OPTION '
write-host -ForegroundColor gray '------------------------------------------------'
write-host -ForegroundColor green 'B) BACKUP'
write-host -ForegroundColor gray '------------------------------------------------'
write-host -ForegroundColor cyan 'R) RESTORE'
write-host -ForegroundColor gray '------------------------------------------------'
write-host -ForegroundColor yellow 'X) Exit'
write-host

$opt = Read-Host "Select an option "
write-host $opt
switch ($opt)
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# End of | PowerShell Script menu
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

{
b { #BACKUP SCRIPT
CLS
write-host "Exporting Reg >"; Reg export ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\CloudStore\Store\Cache\DefaultAccount") ("backup_startmenu.reg") /y
write-host "Collect XML >"; try{Copy-Item ("$env:LOCALAPPDATA\Microsoft\Windows\Shell\DefaultLayouts.xml") -Destination ("backup_startmenu.xml")} #-ErrorAction SilentyContinue
catch{write-host "Failed to find XML"; pause; break}; write-host "The operation completed successfully."
write-host -BackgroundColor yellow -ForegroundColor red " All Done "
pause
}

r { #RESTORE SCRIPT
CLS
write-host "Clean reg >"; Reg delete ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\CloudStore\Store\Cache\DefaultAccount") /f
write-host "Import reg >"; Reg import ("backup_startmenu.reg")
write-host "Place XML >"; try{Copy-Item ("backup_startmenu.xml") -Destination ("$env:LOCALAPPDATA\Microsoft\Windows\Shell\DefaultLayouts.xml")}
catch{write-host "Failed to Place XML"; pause; break}; write-host "The operation completed successfully."

write-host -BackgroundColor yellow -ForegroundColor red " You now need to logout and back in to see the results. "
pause
}

x{#BYE
CLS
write-host "Goodbye, It was nice to see you today."
exit
}

}
}

 

I did find someone has built this process into an application here.  For coded requirements, you can thank me later.

Maybe for personal backups, check out the software version.

Print Friendly, PDF & Email

Powershell Check Registry

The below code looks for a registry ‘String Name’ in a ‘current user’ registry path (Key).

  • HKCU (Current User)
  • HKLM (Local Machine)

If the StringName doesn’t exist, it runs the code in the ‘processing block’. It then writes the exact registry item it looked up in the first place.

IF/When the script is run again it will this time PASS the test and the code block “NO ACTION” will be executed.

 

This function can be added once to the top of your script, then called for multi use later.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Function CheckRegFlag($Func_State,$Func_key,$Func_StringName,$Func_StringValue){
switch($Func_State)
{
start {
$TestPath = (Get-ItemProperty $Func_key) -ne $null
IF ($TestPath -eq "") {
New-Item -Path $Func_key -force | Out-Null
}

$TestValue = (Get-ItemProperty $Func_key).$Func_StringName -ne $null
IF ($TestValue -eq "")
{$Func_WorkNeeded = "Y"; write-host "action needed"}
ELSE
{$Func_WorkNeeded = "N"; write-host "already run"}
}

stop { New-ItemProperty -Path $Func_key -Name $Func_StringName -Value $Func_StringValue |Out-Null; write-host "Saving Flag"}
Default { write-host "No State given"; pause; break}
}
return $Func_WorkNeeded
}

This next bit can be used time and time again to test and complete actions (adding your code to the DO All Commands here)

1
2
3
4
5
6
7
8
9
10
#REG values to check/write
$var_key = ("HKCU:\SOFTWARE\TestKey") #LOCATION OF STRING-NAME IN REG
$var_StringName = ("TestName") #THIS IS WHAT IS CHECKED FOR, NOT THE VALUE.
$var_StringValue = ("TestValue") #THIS IS FOR NOTES

$ActionMe = CheckRegFlag "start" "$var_key" "$var_StringName"
switch($ActionMe){
Y{ write-host "Do all commands here"
CheckRegFlag "stop" "$var_key" "$var_StringName" "$var_StringValue"}
}
Print Friendly, PDF & Email

WMindows 10 Profile backup

It’s been a while and this is shortly explained but the following script is designed to backup a users profile in windows 10 (1903) but should work on all Windows 10.

Line 149: $var_backupRoot needs to be updated to a backup ROOT location.

Download File

 

 

 

 

 

 

 

 

Print Friendly, PDF & Email

VBS Clean Printer list

If your looking to delete all the printers via script this is your code.
You can change the SELECT to pick certain printers.

This .vbs can be deployed using GPO as a user login script.


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where ServerName = '\\\\servername'",,48)
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next

All Printers


("SELECT * FROM Win32_Printer")

All printers where they are offline


("SELECT * FROM Win32_Printer WHERE ExtendedPrinterStatus = 2")

 

 

For more select statements please compile them using the information below.

http://msdn.microsoft.com/en-gb/library/windows/desktop/aa394363(v=vs.85).aspx

http://www.scriptinternals.com/new/us/support/Internal/WMI_Win32_Printer.htm

or search for “WMI Win32_Printer class”

Another useful tool is WMI Code Creator.
This will show you the values of current WMI records and build your select.

Print Friendly, PDF & Email

Check Windows 7 Activation Using VBS

So i had some issues trying to use windows activation and wanted to be able to report on the OS status using BGInfo.

My below script can be referenced  inside BGInfo.

I might be worth downloading WMI Code Creator v1.0 This can be used to show what other information is ready for the picking.

Editing the below script will be able to report on any of the records WMI can throw your way.

 

Copy and paste the following code into a new .vbs file


Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From SoftwareLicensingProduct")
Coltest = int(0)
IsWinActivated = True

For Each objitem In colItems
IF int(objitem.GracePeriodRemaining) = 0 Then Coltest = True else Coltest = False
IF Coltest = False then IsWinActivated = False
Next

IF IsWinActivated = False then
call echo "Failed"
ELSE
call echo "Successful"
END IF

Print Friendly, PDF & Email

Ubuntu 10.4 VPNC Cisco Connection

  • Install vpnc Client
    sudo apt-get install vpnc
  • sudo create file in “/etc/vpnc/” called “*something*.conf” <- creating config file

edit file and place information


IPSec gateway domain.co.uk
IPSec ID GroupID
IPSec secret GroupPassword
Xauth username VPNUsername
Xauth password VPNPassword

  • sudo vpnc-connect *something* <- e.g if file was work.conf then sudo vpnc-connect work
  • sudo vpnc-disconnect <- Disconnect from VPN

Not needed, but SCRIPT login/logout:

  • create file on desktop called “VPN.sh
  • place below script in file and amend connection string.

#!/bin/bash
echo “You Sure you want to connect to
VPN? Press ENTER”
read var_1
sudo vpnc-connect *something*
echo -n “Please ENTER again to disconnect”
read var_2
clear
sudo vpnc-disconnect
echo “VPN has been disconnected”
read var_3

  • Run terminal & navigate to location of VPN.sh
  • Type “chmod 777 VPN.sh” <- Make script executable
  • click “VPN.sh” from desktop and select run in terminal

Links I used when doing this.

  • http://www.ax697.org/writing-a-basic-ubuntu-script-200786.html
  • http://www.longren.org/2007/05/17/how-to-cisco-vpn-client-on-ubuntu-704-feisty-fawn/
  • http://www.gentoo.org/doc/en/vpnc-howto.xml
  • http://ubuntuforums.org/showthread.php?t=16434
Print Friendly, PDF & Email

ASP randomize number

ASP, set randomize


<%
randomize
StoreNumber = int(rnd*100)+1
%>

  • result is a number between 1 and 100
Print Friendly, PDF & Email

Check Registry Settings

Paste the following text into a .vbs file and edit as required.

The code allows you to check registry settings


On Error Resume Next

sResults = ""

Set oWShell = CreateObject("Wscript.Shell")
Call GetAppVersion("Internet Explorer", "HKLM\SOFTWARE\Microsoft\Internet Explorer\Version")
Call  GetAppVersion("Java",  "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{26A24AE4-039D-4CA4-87B4-2F83216016FF}\DisplayVersion")

msgbox sResults

Set oWShell = Nothing

Sub GetAppVersion(sApplication, sRegPath)
On Error Resume Next

sTemp = oWShell.RegRead(sRegPath)
If Err.Number <> 0 Then
sResults = sResults & "Version not found for " & sApplication & "." & vblf
Err.Clear
Else
sResults = sResults & sApplication & " Version: " & sTemp & vblf
End If
End Sub

Print Friendly, PDF & Email

ASP Select description from large string

You can use below code to select the first n of chars from a string.

This can also be done from an ASP string. (not tested but the command is still left.) – Select LEFT(ColumName,1) from TableName where id = ‘100’ ?? Anyone care to confirm?


<%
String1="MyText"
response.write(LEFT(String1,3))
%>

  • Result is MyT
Print Friendly, PDF & Email

Replace vbCrLf with <br>

When you create a textara and then save it to a DB, When you go to use it later on the output page, you will find

that if you used the return key when you wrote your textarea is now on one line. It seems to have foggtten the returns, unless you put it back into a textara.

When in a textarea the return key must be key “vbCrLF” but when in html must be “<br>”. Use this code to switch between the two.

  • replace(inputtext,lookfor,replease with)

<%
Replace(StoredVAR,vbCrLf,"<br>")
%>

Print Friendly, PDF & Email