Change numa group in running process via PowerShell - powershell

I would like to change numa group in running process via PS
all the time is take group 1 :( and cpu work 50%
this - I can do by hand but I don`t know how via PS
my problem is that I have processes that always get assigned to the same group, resulting in 50% of the cpu
Mathias when I add 2 I get this (in the powershell instruction the bitmask controlls number of cpus to in a processor group only - so if the process get's assigned to a group 0, the instruction only changes the number of cpus in this group)

This is not what I call an answer actually (I am not a C++ dev), however, here is a part of the way to probably achieve this :
$MethodDefinition = #'
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetThreadGroupAffinity(......);
'#
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name "Kernel32" -PassThru
#Retrieve the Thread handle first
#Call by $Kernel32::SetThreadGroupAffinity(......)
MS docs SetThreadGroupAffinity,
MS docs GoupAfffinity Structure
May be this one be useful too MS docs SetThreadAffinityMask

You need to set the ProcessorAffinity property on the corresponding process:
Get-Process vmmem |ForEach-Object {
$_.ProcessorAffinity = [IntPtr]::new([long]-1) # -1 corresponds to all 64 bits set
}

Related

Bring a specific window to the front before save automation [duplicate]

As the title says, how can I bring a Powershell GUI window in front of another window after an event has happened, if it is at all possible? As in, I have, for example, Firefox opened and the Powershell GUI is running behind it, after certain event happens inside of the Powershell it pops in front of the Firefox?
On Windows, you can use [Microsoft.VisualBasic.Interaction]::AppActivate() to reactivate your own process' main window via the process ID, as reflected in the automatic $PID variable:
# Enable cross-process window activation (see below).
(Add-Type -ErrorAction Stop -PassThru -Namespace "Random.Ns$PID.AllowWindowActivation" -Name WinApiHelper -MemberDefinition #'
[DllImport("user32.dll", EntryPoint="SystemParametersInfo")]
static extern bool SystemParametersInfo_Set_UInt32(uint uiAction, uint uiParam, UInt32 pvParam, uint fWinIni);
public static void AllowWindowActivation()
{
if (! SystemParametersInfo_Set_UInt32(0x2001 /* SPI_SETFOREGROUNDLOCKTIMEOUT */, 0, 0 /* timeout in secs */, 0 /* non-persistent change */)) {
throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error(), "Unexpected failure calling SystemParametersInfo() with SPI_SETFOREGROUNDLOCKTIMEOUT");
}
}
'#)::AllowWindowActivation()
# Load the required assembly.
Add-Type -AssemblyName Microsoft.VisualBasic
# Launch a sample GUI application that will steal the focus
# (will become the foreground application).
Start-Process notepad.exe
# Wait a little.
Start-Sleep 3
# Now reactivate the main window of the current process.
[Microsoft.VisualBasic.Interaction]::AppActivate($PID)
Note:
Programmatic activation of arbitrary windows across process boundaries is prevented by default:
Instead of the targeted window getting activated, its taskbar button flashes, so as to signal to the user the intent to make the window active.
The Add-Type -MemberDefinition call above overrides this for the current session using a P/Invoke call to the SystemParametersInfo WinAPI function, via setting its SPI_SETFOREGROUNDLOCKTIMEOUT parameter to 0.
This will incur a one-time compilation performance penalty per session.
Cross-process window activation will be enabled for the remainder of the session, for all processes.

How to bring GUI window to front after an event?

As the title says, how can I bring a Powershell GUI window in front of another window after an event has happened, if it is at all possible? As in, I have, for example, Firefox opened and the Powershell GUI is running behind it, after certain event happens inside of the Powershell it pops in front of the Firefox?
On Windows, you can use [Microsoft.VisualBasic.Interaction]::AppActivate() to reactivate your own process' main window via the process ID, as reflected in the automatic $PID variable:
# Enable cross-process window activation (see below).
(Add-Type -ErrorAction Stop -PassThru -Namespace "Random.Ns$PID.AllowWindowActivation" -Name WinApiHelper -MemberDefinition #'
[DllImport("user32.dll", EntryPoint="SystemParametersInfo")]
static extern bool SystemParametersInfo_Set_UInt32(uint uiAction, uint uiParam, UInt32 pvParam, uint fWinIni);
public static void AllowWindowActivation()
{
if (! SystemParametersInfo_Set_UInt32(0x2001 /* SPI_SETFOREGROUNDLOCKTIMEOUT */, 0, 0 /* timeout in secs */, 0 /* non-persistent change */)) {
throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error(), "Unexpected failure calling SystemParametersInfo() with SPI_SETFOREGROUNDLOCKTIMEOUT");
}
}
'#)::AllowWindowActivation()
# Load the required assembly.
Add-Type -AssemblyName Microsoft.VisualBasic
# Launch a sample GUI application that will steal the focus
# (will become the foreground application).
Start-Process notepad.exe
# Wait a little.
Start-Sleep 3
# Now reactivate the main window of the current process.
[Microsoft.VisualBasic.Interaction]::AppActivate($PID)
Note:
Programmatic activation of arbitrary windows across process boundaries is prevented by default:
Instead of the targeted window getting activated, its taskbar button flashes, so as to signal to the user the intent to make the window active.
The Add-Type -MemberDefinition call above overrides this for the current session using a P/Invoke call to the SystemParametersInfo WinAPI function, via setting its SPI_SETFOREGROUNDLOCKTIMEOUT parameter to 0.
This will incur a one-time compilation performance penalty per session.
Cross-process window activation will be enabled for the remainder of the session, for all processes.

Measure actual clock speed via powershell during an intensive task

I have the following script to measure the current cpu clock rate from this [link][1].
$MaxClockSpeed = (Get-CimInstance CIM_Processor).MaxClockSpeed
$ProcessorPerformance = (Get-Counter -Counter "\Processor Information(_Total)\% Processor Performance").CounterSamples.CookedValue
$CurrentClockSpeed = $MaxClockSpeed*($ProcessorPerformance/100)
I am looking to test the performance of the CPU to see how far it can go in terms of frequency.
The reason for this is that we have a few machines that are faulty out of our hundreds of machines, and it turns out that when you push them a bit, their clock rate doesn't change and stays at around 20% utilization. This would allow us, via our monitoring system, to find them easily.
Is there a way to programmatically via powershell to make an intensive task during or just before capturing the actual clock speed to know how far it can get? Something like a loop or something?
[1]: Unable to get current CPU frequency in Powershell or Python
I found something interesting on this website.
So to make the cpu work at 100% for a short time, I can use a background job:
$NumberOfLogicalProcessors = Get-WmiObject win32_processor | Select-Object -ExpandProperty NumberOfLogicalProcessors
ForEach ($core in 1..$NumberOfLogicalProcessors){
start-job -ScriptBlock{
$result = 1;
foreach ($loopnumber in 1..2147483647){
$result=1;
foreach ($loopnumber1 in 1..2147483647){
$result=1;
foreach($number in 1..2147483647){
$result = $result * $number
}
}
$result
}
}
}
Read-Host "Press any key to exit..."
Stop-Job *
It makes the cpu go usually at 100% of utilization. During that time I can easily run the script:
$MaxClockSpeed = (Get-CimInstance CIM_Processor).MaxClockSpeed
$ProcessorPerformance = (Get-Counter -Counter "\Processor Information(_Total)\% Processor Performance").CounterSamples.CookedValue
$CurrentClockSpeed = $MaxClockSpeed*($ProcessorPerformance/100)
For instance, in my case, when I test the $processorPerformance I get "107.998029830411" as an example, so this shows My processor works fine when I push it.
N.B I must add the Start-sleep -Seconds 20 parameter while the background tasks are running, because on all the machines it takes around 15 seconds until the x logical processors are running at 100%.

In ICA Client Object API for Citrix Receiver/Workspace, does setting OutputMode really work?

I follow the specification of Citrix ICA Client Object API Specification
According to this documentation, you can set OutputMode property which has the following meaning:
OutputMode: Output mode for the client engine.
Valid values
0(non-headless),
1(normal),
2(renderless),
3(windowless)
So I set in my code the value to 3 which has the following meaning:
OutputModeWindowless= 3
The client runs as normal, but does not display in the session window. Maintains
internal bitmap surface for screen snapshots. Select this mode to prevent the
client from drawing to the screen if client CPU usage is identified as a
bottleneck. Rendering still occurs in the background to an off-screen surface,
making it possible to obtain screen captures of the session if desired.
But there is absolutely no difference in behaviour, I still see the window as in Normal mode.
I have ensured I set it before connecting as per this documentation:
OutputMode must be defined only at load-time; that is, before a connection is
launched.
I have seen this issue is faced by other developers:
https://discussions.citrix.com/topic/278410-outputmode-windowsless-and-renderless-help-on/
https://discussions.citrix.com/topic/372758-ica-api-icaclientoutputmode-does-not-change-anything/#comment-1904176
https://discussions.citrix.com/topic/372758-ica-api-icaclientoutputmode-does-not-change-anything/#comment-2001192
https://discussions.citrix.com/topic/393456-starting-a-ica-session-in-mode-outputmodewindowless/#comment-2001191
https://discussions.citrix.com/topic/278410-outputmode-windowsless-and-renderless-help-on/#comment-1515471
So question:
Is this method really implemented ?
If yes, what needs to be done to make it work ?
Here is sample code I used:
[system.Reflection.Assembly]::LoadFile("c:\Users\<user>\AppData\Local\Citrix\ICA Client\WfIcaLib.dll")
$icaClient = New-Object WFICALib.ICAClientClass
$icaClient.CacheICAFile = $false
$icaClient.ICAFile = $icapath
$icaClient.OutputMode = [WfIcaLib.OutputMode]::OutputModeWindowless
$icaClient.Launch = $true
$icaClient.TWIMode = $true
$icaClient.Connect()
sleep 10
$enumHandle = $icaClient.EnumerateCCMSessions()
$sessionid = $icaClient.GetEnumNameByIndex($enumHandle, 0)
$icaClient.StartMonitoringCCMSession($sessionid, $true)
#$icaClient.session.ReplayMode = $true
$icaClient.session.Keyboard.SendKeyDown(16) # shift key
$icaClient.session.Keyboard.SendKeyDown(53) # number 5 key
$screenShot = $icaClient.session.CreateFullScreenShot()
$screenShot.Save()
$icaClient.Logoff()
sleep 10
$icaClient.StopMonitoringCCMSession($sessionid)
$icaClient.CloseEnumHandle($enumHandle)
I am using:
Citrix Receiver/Workspace versions I tried: 4.12, 4.9, Workspace 19.11
Citrix StoreFront version: 3.12.5000

How do you get Windows PowerShell to play a sound after .bat job has finished running?

As the title states, I have a .bat job running within PowerShell that when finished running, I would like a sound notification to go off. I was wondering if there was a PowerShell command that I can add to my existing PowerShell command.
In addition to the solutions #TheGameiswar suggests, you can have some fun by making the system actually speak to you:
# Create a new SpVoice objects
$voice = New-Object -ComObject Sapi.spvoice
# Set the speed - positive numbers are faster, negative numbers, slower
$voice.rate = 0
# Say something
$voice.speak("Hey, Harcot, your BAT file is finished!")
Note: I only tested this on Windows 10, so it may not work on other versions, but give it a go and see.
Besides the excellent solutions of boxdog (here) and TheGameiswar (here), I want to mention another possibility, which lets you play some standard system sounds:
[System.Media.SystemSounds]::Asterisk.Play()
[System.Media.SystemSounds]::Beep.Play()
[System.Media.SystemSounds]::Exclamation.Play()
[System.Media.SystemSounds]::Hand.Play()
[System.Media.SystemSounds]::Question.Play()
Another text-to-speech approach
Add-Type -AssemblyName System.Speech
$synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$synth.Speak("Hey $env:USERNAME, your job is finished!")
Customization
For full details, read the docs.
Voice
Select a voice:
$synth.SelectVoice("Microsoft Zira Desktop")
You can view available voices with:
$synth.GetInstalledVoices() | Select-Object -ExpandProperty VoiceInfo
Rate
Set the speaking rate from -10 (slow) through 10 (fast):
$synth.Rate = 5
Volume
Set the volume from 0 (quiet) through 100 (loud):
$synth.Volume = 75
you could use powershell automatic variables to check bat file status ..As per this,$? returns true ,if command is successfull..
below is sample code
$a =Invoke-Command -ScriptBlock {
& "C:\temp1\test.bat"
}
if($?){
[console]::beep(500,300)
}
You could also play custom sounds,
$PlayWav=New-Object System.Media.SoundPlayer
$PlayWav.SoundLocation=’C:\Foo\Soundfile.wav’
$PlayWav.playsync()
references:
https://devblogs.microsoft.com/scripting/powertip-use-powershell-to-play-wav-files/