SHARP Serialport RS232 - powershell

I am trying get Sony TV model from computer over Serialport.
Here is a documentation (page 80 - how to build query, page 83 - how to get model name).
https://cdn.cnetcontent.com/d1/9d/d19d926b-bb36-4ed3-bd03-af1cad4069da.pdf
Here is my simply script, but, unfortunatelly, always return ERR.
What I did wrong?
$hex = 0x4d,0x4e,0x52,0x44,0x31,0x20,0x20,0x20,0x0d
$com = New-Object System.IO.Ports.Serialport COM4,9600,None,8,one
$com.Open()
$com.Write($hex, 0, $hex.Count)
Start-Sleep -s 2
$read = $com.ReadExisting()
Write-Host "Response: $read" #ERR
$com.Close()

Found response: it return ERR wneh power is off.
When I powered on device over RS-232, script starts respond LE740E insteadof ERR

Related

Powershell Hex, Int and Bit flag checking

I am trying to process a flag from the MECM command Get-CMTaskSequenceDeployment called 'AdvertFlags'.
The information from Microsoft in relation to this value is HERE
The value returned is designated as : Data type: UInt32
In the table of flags, the one I need to check is listed as :
Hexadecimal (Bit)
Description
0x00000020 (5)
IMMEDIATE. Announce the advertisement to the user immediately.
As part of my Powershell script I am trying to ascertain if this flag is set.
I can see by converting it to Binary that a particular bit gets set.
When the settings is enabled:
DRIVE:\> [convert]::ToString((Get-CMTaskSequenceDeployment -AdvertisementID ABC20723).AdvertFlags, 2)
100110010000000000100000
When the setting is disabled:
DRIVE:\> [convert]::ToString((Get-CMTaskSequenceDeployment -AdvertisementID ABC20723).AdvertFlags, 2)
100110010000000000000000
The 6th bit is changed. Great! So far though, I've been unable to find a way to check if this bit is set. I suspected something in the bitwise operators (-band -bor etc) would help me here but I've been unable to get it to work.
Any bitwise operation I try returns an error:
"System.UInt64". Error: "Value was either too large or too small for a UInt64."
I mean, I can compare the string literally, but other options may be changed at any point.
Any help greatly appreciated.
EDIT: Just as an example of the error I am seeing, I can see that the bit that is set is '32' and from my limited understanding I should be able to:
PS:\> '100110010000000000100000' -band '32'
Cannot convert value "100110010000000000100000" to type "System.UInt64". Error: "Value was either too large or too small for a UInt64."
At line:1 char:1
+ '100110010000000000100000' -band '32'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastIConvertible
But I just always return an error
To test bit6 in
$AdvertFlags = (Get-CMTaskSequenceDeployment -AdvertisementID ABC20723).AdvertFlags
Should simply be:
if ($AdvertFlags -band 32) { 'bit6 is set' } else { 'bit6 is not set' }
I do not have access to a deployment environment with Get-CMTaskSequenceDeployment cmdlet, nevertheless to confirm what I am stating:
$AdvertFlags = [Convert]::ToUInt32("100110010000000000100000", 2)
$AdvertFlags
10027040
if ($AdvertFlags -band 32) { 'bit6 is set' } else { 'bit6 is not set' }
bit6 is set
$AdvertFlags = [Convert]::ToUInt32("100110010000000000000000", 2)
$AdvertFlags
10027008
if ($AdvertFlags -band 32) { 'bit6 is set' } else { 'bit6 is not set' }
bit6 is not set
Your self-answer using [bigint]'100110010000000000100000' -band "32" to test for bit6 is merely a coincident that it returns the expected value:
10027035..10027045 |ForEach-Object {
$Binary = [convert]::ToString($_, 2)
[pscustomobject]#{
Binary = $Binary
bAnd = $_ -bAnd 32
Bigint = [bigint]$Binary -band "32"
}
}
Yields:
Binary bAnd Bigint
------ ---- ------
100110010000000000011011 0 0
100110010000000000011100 0 0
100110010000000000011101 0 0
100110010000000000011110 0 32 # ← incorrect
100110010000000000011111 0 32 # ← incorrect
100110010000000000100000 32 32
100110010000000000100001 32 32
100110010000000000100010 32 32
100110010000000000100011 32 32
100110010000000000100100 32 0 # ← incorrect
100110010000000000100101 32 0 # ← incorrect
enumerations as flags
But PowerShell has an even nicer way to test them by name:
[Flags()] enum AdvertFlags {
IMMEDIATE = 0x00000020 # Announce the advertisement to the user immediately.
ONSYSTEMSTARTUP = 0x00000100 # Announce the advertisement to the user on system startup.
ONUSERLOGON = 0x00000200 # Announce the advertisement to the user on logon.
ONUSERLOGOFF = 0x00000400 # Announce the advertisement to the user on logoff.
OPTIONALPREDOWNLOAD = 0x00001000 # If the selected architecture and language matches that of the client, the package content will be downloaded in advance
WINDOWS_CE = 0x00008000 # The advertisement is for a device client.
ENABLE_PEER_CACHING = 0x00010000 # This information applies to System Center 2012 Configuration Manager SP1 or later, and System Center 2012 R2 Configuration Manager or later.
DONOT_FALLBACK = 0x00020000 # Do not fall back to unprotected distribution points.
ENABLE_TS_FROM_CD_AND_PXE = 0x00040000 # The task sequence is available to removable media and the pre-boot execution environment (PXE) service point.
APTSINTRANETONLY = 0x00080000 #
OVERRIDE_SERVICE_WINDOWS = 0x00100000 # Override maintenance windows in announcing the advertisement to the user.
REBOOT_OUTSIDE_OF_SERVICE_WINDOWS = 0x00200000 # Reboot outside of maintenance windows.
WAKE_ON_LAN_ENABLED = 0x00400000 # Announce the advertisement to the user with Wake On LAN enabled.
SHOW_PROGRESS = 0x00800000 # Announce the advertisement to the user showing task sequence progress.
NO_DISPLAY = 0x02000000 # The user should not run programs independently of the assignment.
ONSLOWNET = 0x04000000 # Assignments are mandatory over a slow network connection.
TARGETTOWINPE = 0x10000000 # Target this deployment to WinPE only.
HIDDENINWINPE = 0x20000000 # Target this deployment to WinPE only but hide in WinPE. It can only be used by TS variable SMSTSPreferredAdvertID.
}
# $AdvertFlags = [AdvertFlags](Get-CMTaskSequenceDeployment -AdvertisementID ABC20723).AdvertFlags
$AdvertFlags = [AdvertFlags][Convert]::ToUInt32("100110010000000000100000", 2)
# or: $AdvertFlags = [AdvertFlags]('IMMEDIATE', 'ENABLE_PEER_CACHING', 'APTSINTRANETONLY', 'OVERRIDE_SERVICE_WINDOWS', 'SHOW_PROGRESS')
$AdvertFlags
IMMEDIATE, ENABLE_PEER_CACHING, APTSINTRANETONLY, OVERRIDE_SERVICE_WINDOWS, SHOW_PROGRESS
$AdvertFlags -bAnd [AdvertFlags]'IMMEDIATE'
IMMEDIATE
EDIT: My answer here is incorrect as noted above. Leaving here for prosperity!
As always I BELEIVE I found the answer minutes after posting (After spending a couple hours on this!).
By adjusting the type to [bigint] the comparison was able to complete and return the expected answer:
DRIVE:\> [bigint]'100110010000000000100000' -band "32"
32
So a simple:
If (([bigint]'100110010000000000100000' -band "32") -gt 0){$true}else{$false}
True
and:
If (([bigint]'100110010000000000000000' -band "32") -gt 0){$true}else{$false}
False
Solves my issue. Feel free to give any extra advice if this is not the ideal way to proceed.
I though PS would be smarted when auto defining types etc. This is targeting PS5 on Server 2012 R2 though.

Check if extended mode screen in Powershell (need help - translate Autoit to Powershell v2)

I want to translate this AutoIt script to Powershell v2.
(AutoIt is a freeware BASIC-like scripting https://www.autoitscript.com/site/autoit/downloads/ )
#include <WinAPIGdi.au3>
Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & #ComputerName & "\root\cimv2")
Local $colMonitors = $objWMIService.ExecQuery ("Select * from Win32_DesktopMonitor Where Availability=3", "WQL", 0x10 + 0x20)
If NOT IsObj($colMonitors) Then Exit
Local $iCount = 0
For $oMonitor In $colMonitors
$iCount += 1
Next
MsgBox(0, "", "Number of monitors : " & $iCount)
$aMonitor = _WinAPI_EnumDisplayMonitors()
If #error Then Exit
ConsoleWrite ("!! multiscreen mode:" & $aMonitor[0][0]) ;value equal one if there is one screen duplicated or not, if extended the value wille be greater than one. I don’t mind if the computer screen are powered on or not.
If $aMonitor[0][0] > 1 Then
MsgBox(0, "", "extended mode"&$aMonitor[0][0] ) ;give 2
Else
MsgBox(0, "", "duplicate mode"&$aMonitor[0][0] ) ;give 1
EndIf
;---Excerpt from WinAPIGdi.au3 (included with AutoIt) :---
Func _WinAPI_EnumDisplayMonitors($hDC = 0, $tRECT = 0)
Local $hEnumProc = DllCallbackRegister('__EnumDisplayMonitorsProc', 'bool', 'handle;handle;ptr;lparam')
Dim $__g_vEnum[101][2] = [[0]]
Local $aRet = DllCall('user32.dll', 'bool', 'EnumDisplayMonitors', 'handle', $hDC, 'struct*', $tRECT, _
'ptr', DllCallbackGetPtr($hEnumProc), 'lparam', 0)
If #error Or Not $aRet[0] Or Not $__g_vEnum[0][0] Then
$__g_vEnum = #error + 10
EndIf
DllCallbackFree($hEnumProc)
If $__g_vEnum Then Return SetError($__g_vEnum, 0, 0)
__Inc($__g_vEnum, -1)
Return $__g_vEnum
EndFunc ;==>_WinAPI_EnumDisplayMonitors
I tried to begin with this commands in Powershell:
Get-Wmiobject win32_DesktopMonitor |format-list
Get-Wmiobject -query "select Name,DeviceID,DisplayType from win32_DesktopMonitor where Availability=3"
It works but it’s really too short...
Can you please specify what you would like to do? I am not sure what you are trying to achieve.
It works but it’s really too short...
So it works. That's good - right?
If you want to check how many screens are connected you can use the following cmdlet:
Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorBasicDisplayParams
Source: Use PowerShell to Discover Multi-Monitor Information

Powershell UDP script works in ISE but not Console

First time attempting to work with UDP. I'm harvesting lat/long from a NMEA string in a UDP broadcast. The code below works fine in the ISE but hangs up in the console. I'm running both with Admin rights. Other posts on the topic recommend dot-sourcing, no difference. I suspect it's something to do with how it's handling variables but have hit a wall. Thanks in advance for help.
##########################################################################
#
# Read $GPGGA string from UDP broadcast on port 40181
#
# Convert Degree-Minutes-Seconds format to decimal lat/long
#
# UDP code harvested from http://winpowershell.blogspot.com/2010/01/powershell-udp-clientserver.html
#
#
##########################################################################
# Form a connection point
$endpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)
Write-Host "Endpoint: " $endpoint
# Open UDP client
$udpclient = new-Object system.Net.Sockets.Udpclient(40181)
Write-Host "UDP client: " $udpclient
# Grab the datagram
$content = $udpclient.Receive([ref]$endpoint)
Write-Host "Content: " $content
# Put the datagram in $udpstring as type string
$udpstring = [Text.Encoding]::ASCII.GetString($content)
Write-Host "UDP string: " $udpstring
# Close UDP client
$udpclient.Close()
# Get Degree Min Sec format latitude from UDP string
# Explicit typing required in order to do math later
[Double]$DMSlatdeg = $udpstring.Substring(17,2)
$DMSlatmin = $udpstring.Substring(19,2)
# Get North South directional from UDP string
$directNS = $udpstring.Substring(29,1)
# Get Degree Min Sec format longitude from UDP string
# Explicit typing required in order to do math later
[Double]$DMSlongdeg = $udpstring.Substring(31,3)
$DMSlongmin = $udpstring.Substring(34,2)
# Get East West directional from UDP string
$directEW = $udpstring.Substring(44,1)
# Add decimal latitude minutes value to degrees
$declat = ($DMSlatdeg + ($DMSlatmin/60))
# Add decimal longitude minutes value to degrees
$declong = ($DMSlongdeg + ($DMSlongmin/60))
# Output formatted to three decimal places - variable value not changed
Write-Host "Decimal lat: " $declat.ToString("#.###") $directNS
Write-Host "Decimal long: " $declong.ToString("#.###") $directEW
In ISE I get the correct output:
Endpoint: 0.0.0.0:0
UDP client: System.Net.Sockets.UdpClient
Content: 36 71 80 71 71 65 44 [etc]
UDP string: $GPGGA,160516.13,[etc]
Decimal lat: [correct value] N
Decimal long: [correct value] W
In the console, it gets out the first two statements and hangs:
Endpoint: 0.0.0.0:0
UDP client: System.Net.Sockets.UdpClient
Is there something wrong with the way I'm setting $content?

Trouble using Net::Ping

I am using Net::Ping to check the network availability. When I run the code, it returns response. But when I recheck using command prompt, it is time out.
The Code :
$p = Net::Ping->new('tcp');
$p->hires();
($ret, $duration, $ip) = $p->ping($host);
if ($ret){
printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", 1000 * $duration);
}else{
printf("time out");
}
Result :
10.10.x.x [ip: 10.10.x.x] is alive (packet return time: 18.72 ms)
Using command prompt :
C:\ping 10.10.x.x -t
Pinging 10.10.x.x with 32 bytes of data:
Request timed out.
Any explanation and advice would be appreciated

Why can I drive InstallShield with Win32::GuiTest on XP but not Windows 7?

I'm trying to use Win32::GuiTest to test an InstallShield-based uninstall process. I can open the Control Panel, find the application, and invoke InstallShield but nothing I do seems to let me pick the Remove button in the installer. So far, I've got:
sub uninstall($;$) {
my ($name, $force) = #_;
if (! defined($force)) {
$force=0;
}
my #windows;
# Control Panel window
my $cpwin;
my $w;
my $text;
# Install Shield window
my $iswin;
# Run the Control Panel (In windir, do `control appwiz.cpl`)
system("cd %windir% && control appwiz.cpl");
sleep 1;
print("Opened control panel\n");
# Get the Window ID of the control panel
# FIXME - this label is system specifie (W7)
#windows = FindWindowLike(undef, "Programs and Features", "");
$cpwin = $windows[0];
printf("Found CP window ID %x\n", $cpwin);
# Get the Folder View window of the control panel
# Find the list of applications
#windows = FindWindowLike($cpwin, "FolderView");
$w = $windows[0];
# Find program in the list
if (Win32::GuiTest::SelListViewItemText($w, $name) == 0) {
printf("Could not find '$name'.\n");
return -1;
}
# Invoke the installer for by pressing [Return]
Win32::GuiTest::SendKeys("~");
# Wait for the "initializing the wizard" window
#windows = Win32::GuiTest::WaitWindow("InstallShield Wizard", 5);
# Wait for the real installer window
sleep 10;
#windows = Win32::GuiTest::WaitWindow("InstallShield Wizard", 3);
$iswin = $windows[0];
# Win32::GuiTest::WaitWindow("Remove");
printf("Found IS window ID %x\n", $iswin);
# Win32::GuiTest::SetFocus($iswin);
#windows = FindWindowLike($iswin, "&Remove", "Button");
my $remove = $windows[0];
printf("Found remove button %x\n", $remove);
Win32::GuiTest::PushButton($remove);
# Win32::GuiTest::SetFocus($remove);
# Win32::GuiTest::SendKeys("%r");
# Win32::GuiTest::MouseClick("Remove",$iswin);
# Win32::GuiTest::CheckButton($remove);
# Win32::GuiTest::SendKeys("{DOWN}{DOWN}");
# Win32::GuiTest::MouseClick("Next",$iswin);
# Win32::GuiTest::PushChildButton($iswin, "Cancel");
None of the things I've tried (commented out, at the end) seem to have any effect.
I'm using ActivePerl and Win32::GuiTest on Windows 7 if any of that matters.
(Be kind. My Perl probably sucks. I have >25 years experience programming but less than a month in Perl.)
The fact that I'm trying to drive an installer, appears to be a red herring. On XP (even in a VM), this works fine. I suspect that the issue is that the installers present dialog boxes whereas Notepad presents a window and those are somehow handled differently in W7 than XP. I'll come back to why W7 doesn't work but XP is what I have to do right now so that's enough.
The Win32::GuiTest::PushButton method takes the button Text or ID as parameter, not a window/control object. So you don't need to invoke FindwindowLike method at all.
But Win32::GuiTest::PushButton is looking up buttons from the foreground window only, which might be not properly for all the cases. The Win32::GuiTest::PushChildButton should be used.
Please try this way:
##windows = FindWindowLike($iswin, "&Remove", "Button");
#my $remove = $windows[0];
#printf("Found remove button %x\n", $remove);
sleep 10;
Win32::GuiTest::PushChildButton($iswin, "&Remove", 50);