Home

Friday, July 13, 2018

PowerShell - Learning - Entry 1.3

More coding, added getting system information from Dxdiag and system.exe. Along with some more place holder functions. Learning stuff yay! time for sleep before work! Booo sleep!

#Clean up on New Run or on application Exit
if($MainLoopEvent){
    Unregister-Event $MainLoopEvent -Force
    Stop-Job $MainLoopEvent
}

##

#Dummy Data
$SearchPath="$env:USERPROFILE\Desktop"
$SubDir=$false
$MainLoopAction ={
   Get-NewSource
   Update-DataObj
   Update-AppUi
   #Send-Notice
}

##
# "*.*" = All, "*." Directory
Function Make-Watcher($searchPath="$env:USERPROFILE\Desktop",$Filter="*.*", $subDir=$false) {
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = $searchPath
    $watcher.Filter = $Filter
    $watcher.IncludeSubdirectories = $subDir
    $watcher.EnableRaisingEvents = $true
    return $watcher
}

##
Function Set-WatchEvent($Object=$null,$Event="Created",$Action) {
    # Deleted, Renamed, Changed, Created; $changed = Register-ObjectEvent $watcher "Changed" -Action { write-host "Changed: $($eventArgs.FullPath)" }
    if($Object -ne $null){
        $created = Register-ObjectEvent $Object $Event -Action $Action # Watcher/Event & Action Code Source: https://superuser.com/questions/888442/powershell-folder-monitor-batch-execution
            #$action = {Invoke-Item "D:\BATCH FILES\XXXXX.bat" -filter = "XXXXX.pdf"}
        #while ($true) {sleep 5} #Only works if external job
        return $created
    }
}

#Test Functionality
$watcher = Make-Watcher
$MainLoopEvent = Set-WatchEvent $watcher -Action $MainLoopAction

##


##

# Get system info
# $sys | Get-Member -MemberType Property | ForEach{ "{0} = `t{1}" -f $_.name, $sys.($_.name) }

Function Send-Notice ($title="TestTracker Report",$msg="You have a waiting Action. Please Approve the Action.",$bttn='ok',$icon='Exclamation') {
    # Icon Info key Source: MSDN Icon Table: https://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxicon(v=vs.110).aspx
    # TODO: Change frame, change icon to custom, change color; background, header, text
    #$wshell = New-Object -ComObject Wscript.Shell ##$wshell.Popup("Operation Completed",0,"Done",0x1)
    [System.Windows.Forms.MessageBox]::Show($msg,$title,$bttn,$icon) 
}

Function Get-NewSource {
    Write-Host (`
    "`n### `$newSource = Get-Content -Raw -Path `"`$env:USERPROFILE\Desktop\...`"",
    "`$newJson = ConvertFrom-Json `$newSource",
    "return `$newJson")
}

Function Update-DataObj {
    Write-Host "`n### DataObj = NewSourceData"
}

Function Update-AppUi {
    Write-Host "`n### AppUI = UpdatedDataObj"
}

function Get-SystemInfo ($ComputerName=$env:ComputerName,$header = 'Hostname','OSName','OSVersion','OSManufacturer','OSConfig','Buildtype', 'RegisteredOwner','RegisteredOrganization','ProductID','InstallDate', 'StartTime','Manufacturer','Model','Type','Processor','BIOSVersion', 'WindowsFolder' ,'SystemFolder','StartDevice','Culture', 'UICulture', 'TimeZone','PhysicalMemory', 'AvailablePhysicalMemory' , 'MaxVirtualMemory', 'AvailableVirtualMemory','UsedVirtualMemory','PagingFile','Domain' ,'LogonServer','Hotfix','NetworkAdapter')
{
    #param($ComputerName = $env:ComputerName)
    $a = systeminfo.exe /FO CSV /S $ComputerName |
    Select-Object -Skip 1 |
    ConvertFrom-CSV -Header $header
    return $a
    #Flags:
        #/s Computer #Specifies the name or IP address of a remote computer (do not use backslashes). The default is the local computer.
        #/u Domain\User #Runs the command with the account permissions of the user specified by User or Domain\User. The default is the permissions of the current logged on user on the computer issuing the command.
        #/p Password #Specifies the password of the user account that is specified in the /u parameter.
        #/fo {Table|List|CSV} # Specifies the format to use for the output. Valid values are TABLE, LIST, and CSV. The default format for output is LIST.
        #/nh #Suppresses column headers in the output. Valid when the /fo parameter is set to TABLE or CSV.
        # Code Source https://gallery.technet.microsoft.com/scriptcenter/PowerShell-System-571521d1
}

function Get-Dxdiag {
    # Drop output in temp dir
    $logFile = $env:TEMP + "\dxDiagLog.xml"

    # Piping to Out-Null forces it to wait for dxdiag to complete before continuing.  Otherwise
    # it tries to load the file before it actuallygets written
    dxdiag.exe /whql:off /dontskip /x $logFile | Out-Null
    [xml]$dxDiagLog = Get-Content $logFile
    #$dxDiagLog.DxDiag.DirectSound.SoundDevices.SoundDevice | ft Description, DriverProvider
    # CODE SOURCE: https://stackoverflow.com/questions/37122010/can-i-access-dxdiag-from-powershell-console
    return $dxDiagLog
}

function Get-SystemData {
    $S = Get-SystemInfo
    $D = Get-Dxdiag
    $Sys = @{}

    #Gather relevant SystemInformation
    $Sys.add("Date",Get-Date)
    $Sys.add("Computer Name",$env:COMPUTERNAME)
    $Sys.add("OS",$S.OSVersion)
    $Sys.add("Language",$S.Culture)
    $Sys.add("System Manufacturer",$S.Manufacturer)# $S.OSManufacturer
    $Sys.add("System Model",$S.Model)
    $Sys.add("Bios",$S.BIOSVersion)
    $Sys.add("Processor",$D.DxDiag.SystemInformation.Processor)# $S.Processor
    $Sys.add("Memory",$D.DxDiag.SystemInformation.AvaliableOSMem)
    $Sys.add("Page File",$S.PagingFile)
    $Sys.add("DirectX Version",$D.DxDiag.SystemInformation.DirectXVersion)
    $Sys.add("Display Res",$D.DxDiag.DisplayDevices.DisplayDevice[1].CurrentMode)
    $Sys.add("MonitorName",$D.DxDiag.DisplayDevices.DisplayDevice[1].MonitorName)
    $Sys.add("MonitorId",$D.DxDiag.DisplayDevices.DisplayDevice[1].MonitorId)
    $Sys.add("NativeMode",$D.DxDiag.DisplayDevices.DisplayDevice[1].NativeMode)
    return $Sys
}

##


##

Function Set-AutoSession ($Check=$True) {
    if(Check){ Write-Host "`n### Auto is ON." }
    else{ Write-Host "`n### Auto is Off." }
}

Function StartSession {
    Write-Host "`n### Session Started!"
}

Function StopSession {
    Write-Host "`n### Session Stopped!"
}

Function Write-Token ($token=$null,$document=$null) {
    Write-Host "`n### `$token written to `$(`$document[0]) at line `$(`$document[1])."
}

Function Refresh-Token ($token=$null) {
    Write-Host "`n### `$token refreshed."
}


Function Copy-TokenToClipBoard ($token=$null) {
    <#
    Copy-TokenToClipBoard copies a passed token to the Windows Clipboard.
    Syntax; [$token = int,string]
    #>
    #Not Functional yet.
    #Set-Clipboard -Value $token
    Write-Host "`n### `$token copied to clipboard."
}

Function Set-DirectoryPath ($dir="") {
    Write-Host "`n### Path set to `$dir"
}

Function Set-Preferences ($AppPreferences) {
    Write-Host "`n### Preference saved!"
    $AppPreferences | Out-file $outFile
}

##

No comments:

Post a Comment

Conduct: Be nice and don't advertise or plug without adding value to the conversation.