I am using the following code to create a Network Location, but the result is not exactly the same as when doing it manually. The icon from shell32.dll is smaller, lower res and has a frame, as seen here on the left. Is there any way to match the "native" look with PowerShell?
$linkFolder = New-Item -name:$location.name -path:"$nethoodPath\$($location.name)" -type:Directory -errorAction:stop
# Create the ini file
$desktopIniContent = (
'[.ShellClassInfo]',
'CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}',
'Flags=2',
'ConfirmFileOp=1'
) -join "`r`n"
$desktopIniContent | Out-File -filePath:"$nethoodPath\$($location.name)\Desktop.ini"
# Create the shortcut file
$link = $shell.Createshortcut("$nethoodPath\$($location.name)\target.lnk")
$link.TargetPath = $location.value
$link.IconLocation = "%SystemRoot%\system32\SHELL32.DLL, 275"
$link.Description = $location.value
$link.WorkingDirectory = $location.value
$link.Save()
# Set attributes on the files & folders
Set-ItemProperty "$nethoodPath\$($location.name)\Desktop.ini" -name:Attributes -value:([IO.FileAttributes]::System -bxor [IO.FileAttributes]::Hidden) -errorAction:stop
Set-ItemProperty "$nethoodPath\$($location.name)" -name:Attributes -value:([IO.FileAttributes]::ReadOnly) -errorAction:stop
The icon on the right hand side can be found in imageres.dll (the Windows image resource library), icon 138 (index 137):
$link.IconLocation = "%SystemRoot%\system32\imageres,137"
It would seem that the horrible resolution of the shell32.dll,275 version is a bug/blunder that has been fixed by Microsoft subsequently:
Windows 7:
The shell32.dll version scales horribly
Windows 10:
On Windows 10, shell32.dll,275 scales to "Extra Large Icons"-size perfectly fine
Related
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.
I have a file as below.
I want it to convert it to CSV and want to have the out grid view of it for items Drives,Drive Type,Total Space, Current allocation and Remaining space only.
PS C:\> echo $fileSys
Storage system address: 127.0.0.1
Storage system port: 443
HTTPS connection
1: Name = Extreme Performance
Drives = 46 x 3.8T SAS Flash 4
Drive type = SAS Flash
RAID level = 5
Stripe length = 13
Total space = 149464056594432 (135.9T)
Current allocation = 108824270733312 (98.9T)
Remaining space = 40639785861120 (36.9T)
I am new to Powershell but I have tried below code for two of things but it's not even getting me desired output.
$filesys | ForEach-Object {
if ($_ -match '^.+?(?<Total space>[0-9A-F]{4}\.[0-9A-F]{4}\.[0-9A-F]{4}).+?(?<Current allocation>\d+)$') {
[PsCustomObject]#{
'Total space' = $matches['Total space']
'Current allocation' = $matches['Current allocation']
}
}
}
First and foremost, the named capture groups cannot contain spaces.
From the documentation
Named Matched Subexpressions
where name is a valid group name, and subexpression is any valid
regular expression pattern. name must not contain any punctuation
characters and cannot begin with a number.
Assuming this is a single string since your pattern attempts to grab info from multiple lines, you can forego the loop. However, even with that corrected, your pattern does not appear to match the data. It's not clear to me what you are trying to match or your desired output. Hopefully this will get you on the right track.
$filesys = #'
Storage system address: 127.0.0.1
Storage system port: 443
HTTPS connection
1: Name = Extreme Performance
Drives = 46 x 3.8T SAS Flash 4
Drive type = SAS Flash
RAID level = 5
Stripe length = 13
Total space = 149464056594432 (135.9T)
Current allocation = 108824270733312 (98.9T)
Remaining space = 40639785861120 (36.9T)
'#
if($filesys -match '(?s).+total space\s+=\s(?<totalspace>.+?)(?=\r?\n).+allocation\s+=\s(?<currentallocation>.+?)(?=\r?\n)')
{
[PsCustomObject]#{
'Total space' = $matches['totalspace']
'Current allocation' = $matches['currentallocation']
}
}
Total space Current allocation
----------- ------------------
149464056594432 (135.9T) 108824270733312 (98.9T)
Edit
If you just want the values in the parenthesis, modifying to this will achieve it.
if($filesys -match '(?s).+total space.+\((?<totalspace>.+?)(?=\)).+allocation.+\((?<currentallocation>.+?)(?=\))')
{
[PsCustomObject]#{
'Total space' = $matches['totalspace']
'Current allocation' = $matches['currentallocation']
}
}
Total space Current allocation
----------- ------------------
135.9T 36.9T
$unity=[Regex]::Matches($filesys, "\(([^)]*)\)") -replace '[(\)]','' -replace "T",""
$UnityCapacity = [pscustomobject][ordered] #{
Name = "$Display"
"Total" =$unity[0]
"Used" = $unity[1]
"Free" = $unity[2]
'Used %' = [math]::Round(($unity[1] / $unity[0])*100,2)
}``
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
if I have some files with the name pippo_yyyymmdd.txt, pluto_yyyymmdd.txt etc etc, how can I remove the timestamp and rename the files as pippo.txt, pluto.txt ?
Give this a try, it will remove any underscore followed by 8 digits. This is tha basicmidea, it doesn't take care of files end up having the same name:
Get-ChildItem *.txt |
Where {$_.Name -match '_\d{8}\.txt' } |
Rename-Item -NewName {$_.Name -replace '_\d{8}'}
I was facing the same problem but unfortunately, PowerShell did not help me to solve it. So I then resorted to using Python to solve this problem and it worked like a bomb. You only need to copy the python script into the affected folder and run the script to remove the timestamp from all the files in the folder and subfolders.
import os, sys, re
fileNameList =[]
patho = os.path.dirname(os.path.realpath(sys.argv[0]))
def renamer(fpath):
for path, subdirs, files in os.walk(fpath):
for name in files:
if re.findall(r"(?ix)(\.ini)", name, re.I) == None:
if re.search(r"(?ix)(\(\d{4}\_\d{2}_\d{2}\s\d{2}\_\d{2}\_\d{2}\sutc\))", name, re.I) != None:
old_name = os.path.join(path,name)
print(old_name)
new_name = re.sub(r"(?ix)(\s\(\d{4}\_\d{2}_\d{2}\s\d{2}\_\d{2}\_\d{2}\sutc\))", "", old_name, re.I)
print(new_name)
try:
os.replace(old_name,new_name)
except:
print(old_name)
fileNameList.append(old_name)
def log_errors_directories(fileNameList):
filename = "Log of error filenames.txt"
if len(fileNameList) != 0:
log = open(filename, "a")
log.write("###########################################################\n")
i = 1
for line in fileNameList:
nr_line = str(i) + " " + str(line)
log.write(str(nr_line))
log.write("\n")
i += 1
log.write("###########################################################\n\n")
log.close()
renamer(patho)
log_errors_directories(fileNameList)
This is my first time posting code online. I hope it works for you as it did for me. :)
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);