Testing Some Basics in C# – Part 2 of 2

email me

Part 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Decisions
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Behind the Door!";
string Success = "1";
string message = "";
string UserValue = "";
Console.WriteLine("");

do
{
Console.WriteLine("Eddie's TV Show");
Console.WriteLine("Choose a door: 1, 2, 3: ");
UserValue = Console.ReadLine();

if (UserValue == "1")
{
message = "You have won a vacation to Hawaii!";
Success = "0";
}
else if (UserValue == "2")
{
message = "You have won a jetski!";
Success = "0";
}
else if (UserValue == "3")
{
message = "You have won a cruise!";
Success = "0";
}
else
{
Console.WriteLine("");
Console.WriteLine("I did not recognize your answer!");
System.Threading.Thread.Sleep(3000);
Console.Clear();

}
Console.WriteLine(message);

} while (Success == "1");

Console.WriteLine("");
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();

}
}
}

Install Windows Updates Remotely

email me

If you have ever had to install Windows updates, as in patching servers, you know you have to log into servers and allow updates to install, suppressing reboots along the way. To remotely install updates, try some of these methods.

PowerShell

Invoke-Command -ComputerName ServerName {wusa.exe C:\Updates\Windows6.1-KB894199-x64.msu /quiet /norestart}

 
 

VBScript

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

set filetxt = objFSO.OpenTextFile("C:\Servers.txt",1)
strPSExec = "C:\PSExec.exe"
rbcopy = "c:\Windows\System32\Robocopy.exe"

strPSExec = objFSO.GetFile(strPSExec).ShortPath
rbcopy = objFSO.GetFile(rbcopy).ShortPath

Do Until filetxt.AtEndOfStream
strServerName = filetxt.Readline
strServerName = trim(strServerName)

'Copy updates
strCmd = "%comspec% /c " & rbcopy & " C:\Updates" & " \\" & strServerName & "\c$\Updates"
WScript.echo strCmd
objShell.Run strCmd, 1, True

'Run updates
strCmd = "cmd /C " & strPSExec & " \\" & strServerName & " ""C:\Updates\"" /quiet"
WScript.echo strcmd
objShell.Run strCmd, 1, True

Loop

 
 

Command in Script

psexec \\ServerName -c -s -u Domain\Administrator -p P@$$W0rd \\ShareName\PathToWUInstall\WUInstall.exe /install

 
 

Notes

If you have issues with PSExec, try PAExec.

wusa.exe “\\ServerName\winupdates\windows6.1-kb894199-x64.msu”

https://blogs.technet.microsoft.com/heyscriptingguy/2011/08/13/use-powershell-to-audit-and-install-windows-patches/

https://gallery.technet.microsoft.com/scriptcenter/Force-Install-Updates-on-ef821f9a

Some PowerShell Basics

email me

You ever needed just to write a quick script, but you don’t have time to learn the ins and outs of scripting? Well, here are some PowerShell basics to get you up and running. By using the code here, you can quickly create some simple scripts. Just open notepad, paste the segments you need, update the paths and/or file names, and save as script.ps1.

Let me know if you’d like to see something else.

#========================================================================
# Created on: 10/13/2016 11:48 AM
# Created by: Eddie Jackson
# Organization: Commlink Industries
# Filename: script.ps1
#========================================================================

# Create Registry Key
$Date = Date
New-ItemProperty -Path HKCU:\Contoso.com\App1 -Name Timestamp -Value "$Date" -Force

# Read Registry Key
$RegPath = "HKCU:\Contoso.com\App1"
$Reg = Get-ItemProperty $RegPath -Name Timestamp
$(New-Object -comobject "WScript.Shell").Popup($Reg,0,"Reg Key",0)

# Delete Registry Key
$RegPath = "HKCU:\Contoso.com\App1"
$Reg = Remove-ItemProperty $RegPath -Name Timestamp
$(New-Object -comobject "WScript.Shell").Popup("The Reg Key was deleted!",0,"Reg Key",0)

# Copy File
$SourceFile = "Test.txt"
$NewFile = "Test2.txt"
if ([System.IO.File]::Exists($SourceFile)) {
Copy-Item $SourceFile $NewFile
"Source File ($SourceFile) copied to ($newFile)"
}

# Delete File
$FileName = "test3.txt"
if ([System.IO.File]::Exists($FileName)) {
Remove-Item $FileName
}

# Check Folder, Create or Delete Folder
$FolderPath = "c:\_PS\sample1\test\"
if ([system.io.Directory]::Exists($FolderPath)) {
$(New-Object -comobject "WScript.Shell").Popup("That folder does exists!",0,"Folder Path",0)
Remove-Item -Path $FolderPath
$(New-Object -comobject "WScript.Shell").Popup("The folder was deleted!",0,"Folder Path",0)
}

Else {
$(New-Object -comobject "WScript.Shell").Popup("That folder does not exist",0,"Folder Path",0)
New-Item -path $FolderPath -itemType "directory"
$(New-Object -comobject "WScript.Shell").Popup("The folder was created!",0,"Folder Path",0)
}

# Create text file
$CreateFile | Out-File 'test3.txt'

# Writing to a text file
$AddTextToFile = 'This is some sample text'
$AddTextToFile | Out-File 'test3.txt'

# Appending to a text file
$AddTextToFile = 'This is another line'
$AddTextToFile | Out-File 'test3.txt' -Append

# Reading from a text file
$TheTextFile = Get-Content test3.txt
$(New-Object -comobject "WScript.Shell").Popup($TheTextFile,0,"Contents of file",0)

# Create Event Log
New-Eventlog -Logname Application -Source 'AppName1' -ErrorAction SilentlyContinue
Write-Eventlog -Logname Application -Message 'Your Message Goes Here' -Source 'AppName1' -id 777 -entrytype Information -Category 0

# Launch Process
Start-Process notepad.exe
#-Wait -WindowStyle Maximized

Tableau Automation

email me

Command Line Tool Options and Script Automation

Using scripting languages or manual operations via the Command Prompt, you can activate online, refresh, and deactivate your license keys. For Tableau Desktop, the application itself provides the command line interface. For Tableau Server, the executable is named tabinstallck.exe and can be used prior to configuring Tableau Server.

When performing quiet installations of Tableau Desktop or Tableau Server, use the online activation parameter to avoid having Tableau prompt the user to manually enter the license key for each installation. In addition, you can use the “return” parameter to retrieve a license and deactivate it without having to manually remove it from an individual machine.

Note: To successfully use the licensing parameters, you must run scripts or perform manual operations as an administrator. For me, I used desktop management software to deploy a compiled script, which ran in the system account.

Task Commands
Online activation Desktop (Windows) start /wait tableau.exe -activate <license-key>
Desktop (Mac) ./Applications/Tableau.app/Contents/MacOS/Tableau -activate <license key>
Server start/wait tabinstallck.exe-activate <license-key>
Refreshing license key Desktop (Windows) start /wait tableau.exe -refresh <license-key>
Desktop (Mac) ./Applications/Tableau.app/Contents/MacOS/Tableau -refresh <license key>
Server start /wait tabinstallck.exe -refresh <license-key>
Deactivating license key Desktop (Windows) start /wait tableau.exe -return <license-key>
Desktop (Mac) ./Applications/Tableau.app/Contents/MacOS/Tableau -return <license key>
Server start /wait tabinstallck.exe -return <license-key>

 

For example, a script that could be used to activate Tableau Desktop on a Windows computer would look like the following:

Batch Script

@echo on
Title Deactivate Tableau
cls
Echo Deactivating...
tableau.exe -return FD1Q-6QME-25V9-575F-27FPC
ping -n 4 127.0.0.1>nul
cls
echo Deactivation Complete!
ping -n 4 127.0.0.1>nul
exit /b 0

Dealing with the registration Pop up

AutoIT Script

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#region ---Au3Recorder generated code Start (v3.3.7.0) ---

#region --- Internal functions Au3Recorder Start ---

Func _Au3RecordSetup()
Opt('WinWaitDelay',100)
Opt('WinDetectHiddenText',1)
Opt('MouseCoordMode',0)
EndFunc

Func _WinWaitActivate($title,$text,$timeout=0)
WinWait($title,$text,$timeout)
If Not WinActive($title,$text) Then WinActivate($title,$text)
WinWaitActive($title,$text,$timeout)
EndFunc

_AU3RecordSetup()

RegWrite("HKCU\Software\Tableau\Registration\Data", "company", "REG_SZ", "ABC")
RegWrite("HKCU\Software\Tableau\Registration\Data", "country", "REG_SZ", "FR")
RegWrite("HKCU\Software\Tableau\Registration\Data", "email", "REG_SZ", "ABC@xya.com")
RegWrite("HKCU\Software\Tableau\Registration\Data", "first_name", "REG_SZ", "ABD")
RegWrite("HKCU\Software\Tableau\Registration\Data", "last_name", "REG_SZ", "XYZ")

#endregion --- Internal functions Au3Recorder End ---

Local $Ostype = RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\", "PROCESSOR_ARCHITECTURE")

If $Ostype = "X86" Then
Run('C:\Program Files\Tableau\Tableau Reader 9.3\bin\tabreader.exe')
Else
Run('C:\Program Files (x86)\Tableau\Tableau Reader 9.3\bin\tabreader.exe')
EndIf

_WinWaitActivate("[Class:Qt5QWindowIcon]","",1)
Send("{ENTER}{ENTER}{ENTER}")
Local $PID = ProcessExists("tabreader.exe")
If $PID Then ProcessClose($PID)

 

Yet, another script

#include <MsgBoxConstants.au3>
BlockInput(1)

Run("C:\Program Files\Tableau\Tableau Reader 10.0\bin\tabreader.exe")
Global $i = 0

Sleep(6000)
;Main Timing
WinWait("Activate Tableau Reader", "", 30)

Sleep(1000)
DO
If WinExists("Activate Tableau Reader") Then
WinActivate("Activate Tableau Reader")
Send("ABC")
;Sleep(50)
WinActivate("Activate Tableau Reader")
Send("{TAB}")

WinActivate("Activate Tableau Reader")
Send("Inc")
;Sleep(50)
WinActivate("Activate Tableau Reader")
Send("{TAB}")

WinActivate("Activate Tableau Reader")
Send("Username")
;Sleep(50)
WinActivate("Activate Tableau Reader")
Send("{TAB}")

WinActivate("Activate Tableau Reader")
Send("You@Computer.com")
;Sleep(50)
WinActivate("Activate Tableau Reader")
Send("{TAB}")

WinActivate("Activate Tableau Reader")
Send("12345")
;Sleep(50)
WinActivate("Activate Tableau Reader")
Send("{TAB}")

WinActivate("Activate Tableau Reader")
Send("U")
;Sleep(50)
WinActivate("Activate Tableau Reader")
Send("{TAB}")

WinActivate("Activate Tableau Reader")
Send("F")
;Sleep(50)
WinActivate("Activate Tableau Reader")
Send("{TAB}")
Send("{ENTER}")
ExitLoop
$i = 20
EndIf
$i = $i + 1
;Sleep(1000)
UNTIL $i &gt; 14

BlockInput(0)
Run("taskkill /f /im tabreader.exe", "",@SW_HIDE)
Sleep(1000)

MsgBox($MB_OK, "Tableau 10.0", " Tableau has been activated.")

;@SW_HIDE = Hidden window (or Default keyword)
;@SW_MINIMIZE = Minimized window
;@SW_MAXIMIZE = Maximized window

 

VBScript Method

on error resume next

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Const HKEY_LOCAL_MACHINE = &H80000002
Const OverwriteExisting = TRUE
'SETS CURRENT DIRECTORY TO VARIABLE
strCurrentDirectory = objShell.CurrentDirectory
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objRegistry=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\ProfileList"
objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
For Each objSubkey In arrSubkeys
on error resume next
strValueName = "ProfileImagePath"
strSubPath = strKeyPath & "\" & objSubkey
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strSubPath,strValueName,strValue
Const POPUP_TITLE = "User To SID Conversion"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objAccount = objWMIService.Get("Win32_SID.SID='" & objSubkey & "'")
strUser = objAccount.AccountName
'strDomain = objAccount.ReferencedDomainName'returns referenced domain

'DISPLAY PROFILE NAME & SID
objSubkey = trim(objSubkey)'trims whitespace
strUser = trim(strUser)'trims whitespace
'msgbox "objSubkey: " & objSubkey'returns SID
'msgbox strUser'returns username

'LOGIC TO DETERMINE IF REGISTRY ACCOUNT IS TO BE LOADED
if strUser = "SYSTEM" then strUser=""
if strUser = "LOCAL SERVICE" then strUser=""
if strUser = "NETWORK SERVICE" then strUser=""
if strUser = "ADMINISTRATOR" then strUser=""

if strUser <> "" then
on error resume next
Wscript.Sleep 1000

'ADD REG KEYS
objShell.Run "%comspec% /c reg.exe add "&chr(34)&"HKEY_USERS\" & objSubkey & "\Software\Tableau\Registration\Data"&chr(34)&" /t REG_SZ /v ""company"" /d ""ABC Inc"" /f",0,true
objShell.Run "%comspec% /c reg.exe add "&chr(34)&"HKEY_USERS\" & objSubkey & "\Software\Tableau\Registration\Data"&chr(34)&" /t REG_SZ /v ""country"" /d ""US"" /f",0,true
objShell.Run "%comspec% /c reg.exe add "&chr(34)&"HKEY_USERS\" & objSubkey & "\Software\Tableau\Registration\Data"&chr(34)&" /t REG_SZ /v ""email"" /d ""You@email.com"" /f",0,true
objShell.Run "%comspec% /c reg.exe add "&chr(34)&"HKEY_USERS\" & objSubkey & "\Software\Tableau\Registration\Data"&chr(34)&" /t REG_SZ /v ""first_name"" /d ""User"" /f",0,true
objShell.Run "%comspec% /c reg.exe add "&chr(34)&"HKEY_USERS\" & objSubkey & "\Software\Tableau\Registration\Data"&chr(34)&" /t REG_SZ /v ""last_name"" /d ""Inc"" /f",0,true
objShell.Run "%comspec% /c reg.exe add "&chr(34)&"HKEY_USERS\" & objSubkey & "\Software\Tableau\Registration\Data"&chr(34)&" /t REG_SZ /v ""state"" /d ""TX"" /f",0,true

objShell.Run "%comspec% /c reg.exe add "&chr(34)&"HKEY_USERS\" & objSubkey & "\Software\Tableau\Tableau Reader 8.1"&chr(34)&" /t REG_SZ /v ""DefaultsInstalled"" /d ""8100.14.0510.1702"" /f",0,true

objShell.Run "%comspec% /c reg.exe add "&chr(34)&"HKEY_USERS\" & objSubkey & "\Software\Tableau\Tableau Reader 8.1\LicenseCache"&chr(34)&" /t REG_SZ /v ""Desktop"" /d ""ew752794|<license-cache><map key='feature' value='' /><map key='signature' value='' /><map key='maintenance-date' value='12/31/2028 12:00:00 AM' /><map key='expiration-date' value='12/31/2028 12:00:00 AM' /><map key='vendor-string' value='CAP=NOLICUI,NOSERVER,REG:SHORTLOC,WARN:0;DC_CAP=;DC_STD=default;EDITION=Standard;MAP_CAP=;MAP_STD=default;OEMNAME=;OFFLINE=true;TRIALVER=' /></license-cache>"" /f",0,true

end if

Next