dmidecode | awk ' BEGIN { printf "{" first_entry = 0 is_array = 0 array_first_entry = 0 } { if ($0 ~ /^Handle/) { if (is_array) { printf "]" is_array = 0 } if (first_entry) { printf "}," } first_entry = 1 gsub(/,$/, "", $2); printf "\"%s\": {", $2 getline line printf "\"_description\": \"%s\"", line gsub(/,$/, "", $5); printf ",\"_type\": \"%s\"", $5 gsub(/,$/, "", $6); printf ",\"_bytes\": \"%s\"", $6 } else if ($0 ~ /:/) { if (is_array) { printf "]" is_array = 0 } gsub(/^[[:space:]]+/, "", $0) split($0, a, ": "); if (length(a) > 1) { gsub(/"/, "\\\"", a[2]) gsub(/[[:space:]]+$/, "", a[2]) printf ",\"%s\": \"%s\"", a[1], a[2] } else if (length(a) == 1) { gsub(/:$/, "", a[1]) printf ",\"%s\": [", a[1] is_array = 1 array_first_entry = 0 } } else if (is_array && NF > 0) { if (array_first_entry) { printf "," } array_first_entry = 1 gsub(/^[[:space:]]+/, "", $0) gsub(/"/, "\\\"", $0) printf "\"%s\"", $0 } } END { if (is_array) { printf "]" } print "}}" }'
Category Archives: script
Powershell Mysql helper functions
Some Powershell functions for Preparing Mysql Statements and executing them.
Requires the MySQL .NET Connector
$Dbserv = "server" $Dbname = "db" $Dbuser = "user" $Dbpass = "pass" #MySQL Database connection [System.Reflection.Assembly]::LoadWithPartialName("MySql.Data") | Out-Null $SQLConnection = New-Object MySql.Data.MySqlClient.MySqlConnection $SQLConnection.ConnectionString = "server=$Dbserv;port=3306;uid=$Dbuser;pwd=$Dbpass;database=$Dbname;Integrated Security=False" function MySQLprepare { [OutputType([MySql.Data.MySqlClient.MySqlCommand])] param ( # Query [Parameter( Mandatory = $true )] [String] $query, # Connection [Parameter( Mandatory = $true, ValueFromPipeline = $true )] [MySql.Data.MySqlClient.MySqlConnection] $connection ) $command = New-Object MySql.Data.MySqlClient.MySqlCommand $command.Connection = $connection $command.CommandText = $query (Select-String "@\w+" -input $query -AllMatches).Matches.Value | ForEach-Object { if ($_) { $command.Parameters.AddWithValue($_, "") } } | Out-Null $command.Prepare() return , $command } function MySQLexec { [OutputType([Int])] param ( # QueryParams [Parameter( Mandatory = $false )] [hashtable] $queryParams = @{}, # Command [Parameter( Mandatory = $true, ValueFromPipeline = $true )] [MySql.Data.MySqlClient.MySqlCommand] $command ) (Select-String "@\w+" -input $command.CommandText -AllMatches).Matches.Value | ForEach-Object { if ($_) { $command.Parameters[$_].Value = $queryParams[$_] } } | Out-Null $affectedRows = $command.ExecuteNonQuery() return $affectedRows } function MySQLquery { [OutputType([System.Data.DataTable])] param ( # QueryParams [Parameter( Mandatory = $false )] [hashtable] $queryParams = @{}, # Command [Parameter( Mandatory = $true, ValueFromPipeline = $true )] [MySql.Data.MySqlClient.MySqlCommand] $command ) (Select-String "@\w+" -input $command.CommandText -AllMatches).Matches.Value | ForEach-Object { if ($_) { $command.Parameters[$_].Value = $queryParams[$_] } } | Out-Null $dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command) $dataSet = New-Object System.Data.DataSet $dataAdapter.Fill($dataSet, "data") | Out-Null return , $dataSet.Tables["data"] }
Examples
Preparing some statements
$insertVM = $SQLConnection | MySQLprepare -query "INSERT INTO virtual_machines ( id, name, description, status ) VALUES ( @id, @name, @description, 'new' );" $selectVMs = $SQLConnection | MySQLprepare -query "SELECT * FROM virtual_machines WHERE status LIKE @status;" $updateVM = $SQLConnection | MySQLprepare -query "UPDATE virtual_machines SET status = @status WHERE id = @id;"
Executing them
$insertVM | MySQLexec -queryParams @{"@id" = 1; "@name" = 'test'; "@description" = 'example vm'} | Out-Null $dtVMs = $selectVMs | MySQLquery -queryParams @{"@status" = 'new'} foreach ($vm in $dtVMs.Rows) { Write-Host "Working on VM" $vm["id"] -ForegroundColor Cyan $updateVM | MySQLexec -queryParams @{"@id" = $vm["id"]; "@status" = 'ready'} | Out-Null }
bash DNSDIST dashboard
bat messagebox / jscript embedded in bat
@if (@x)==(@y) @end /***** jscript comment ****** @echo off cscript //E:JScript //nologo "%~f0" "%~nx0" %* exit /b 0 @if (@x)==(@y) @end ****** end comment *********/ /* Value Button 0 OK 1 OK, Cancel 2 Abort, Ignore, Retry 3 Yes, No, Cancel 4 Yes, No 5 Retry, Cancel Value Icon 0 No Icon 16 Critical 32 Question 48 Exclamation 64 Information */ var icon = 64; var button = 0; var title = "Sample Title"; var message = "Sample Message"; var timeout = 30; var pressed_message = "button pressed"; var timeout_message = "timedout"; function runPopup(){ var wshShell = WScript.CreateObject("WScript.Shell"); var btn = wshShell.Popup(message, timeout, title, button + icon + 0x10000); /* Value Return Code 1 OK 2 Cancel 3 Abort 4 Retry 5 Ignore 6 Yes 7 No -1 None, message box was dismissed automatically (timeout) */ switch(btn) { // button pressed. case 1: WScript.Echo(pressed_message); break; // Timed out. case -1: WScript.Echo(timeout_message); break; } } runPopup();
vbs http download
Dim Hostname, fso, CurrentDirectory Set wshShell = Wscript.CreateObject("Wscript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") CurrentDirectory = fso.GetAbsolutePathName(".") Hostname = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") WScript.Echo "HOSTNAME: " & Hostname HTTPDownload "http://example.com/" & Hostname & ".txt", CurrentDirectory Sub HTTPDownload(myURL, myPath) ' Standard housekeeping Dim i, objFile, objFSO, objHTTP, strFile Const ForReading = 1, ForWriting = 2, ForAppending = 8 ' Create a File System Object Set objFSO = CreateObject("Scripting.FileSystemObject") ' Check if the specified target file or folder exists, ' and build the fully qualified path of the target file If objFSO.FolderExists(myPath) Then strFile = objFSO.BuildPath(myPath, Mid(myURL, InStrRev(myURL, "/") + 1)) ElseIf objFSO.FolderExists(Left(myPath, InStrRev(myPath, "\") - 1)) Then strFile = myPath Else WScript.Echo "ERROR: Target folder not found." Exit Sub End If ' Create or open the target file Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True) ' Create an HTTP object Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") ' Download the specified URL objHTTP.Open "GET", myURL, False objHTTP.Send ' Write the downloaded byte stream to the target file For i = 1 To LenB(objHTTP.ResponseBody) objFile.Write Chr(AscB(MidB(objHTTP.ResponseBody, i, 1))) Next ' Close the target file objFile.Close() End Sub