Setting Affinity of an Application on cmd - cmd

I have this problem when I'm using Voicemeeter and Discord together, my voice is just crackling and cutting out. I found out the solution to this problem. It's by going to the task manager, heading to details, right clicking audiodg.exe, and then setting it's affinity to only one processer. The problem is I don't want to do this all by hand everytime I start my computer. Is there any way I can write a line of code into the cmd that changes this? This way I can save this lane as a bat file and then put it into the shell:startup and everytime I turn my computer on it will do it automatically for me.
Thank you so much in advance.
Edit:
I'm sorry I wasn't aware of that. This is the error I get:
C:\Users\borah>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
"$Process = Get-Process audiodg.exe; $Process.ProcessorAffinity=1"
Get-Process : Cannot find a process with the name "audiodg.exe".
Verify the process name and call the cmdlet again. At line:1 char:12
$Process = Get-Process audiodg.exe; $Process.ProcessorAffinity=1
~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (audiodg.exe:String) [Get-Process], ProcessCommandException
FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
The property 'ProcessorAffinity' cannot be found on this object.
Verify that the property exists and can be set. At line:1 char:37
$Process = Get-Process audiodg.exe; $Process.ProcessorAffinity=1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : PropertyNotFound

You can set affinity by typing a command, like following,
Start /affinity 2 notepad
This will start notepad while setting an affinity to second core of CPU.
You can save the command above in a file ending with .cmd and run it by double clicking like any other application. Or you can use task scheduler to launch it with windows startup every time, or you can run it as a startup application.
Edit
If you meant changing affinity of an already running process. Check Mofi's comment as solution.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "$Process = Get-Process audiodg; $Process.ProcessorAffinity=1"

Related

Launching multiple RDP sessions via powershell script

Here is the script:
$compnames = import-csv "$env:userprofile\Documents\list.csv"
$User = "username"
$Password = "password"
foreach ($computer in $compnames) {
cmdkey /generic:TERMSRV/$($computer.compname) /user:$User /pass:$Password
mstsc /v:$($computer.compname)
}
When I run the script, I get the following error:
cmdkey : The term 'cmdkey' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Program Files\WindowsPowerShell\Modules\rdpsession\1.0.2\RdpSession.psm1:92 char:1
+ cmdkey /generic:Termsrv/$computer /user:$Username /pass:$rdp > $null
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (cmdkey:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
mstsc : The term 'mstsc' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Program Files\WindowsPowerShell\Modules\rdpsession\1.0.2\RdpSession.psm1:94 char:1
+ mstsc /v:$computer
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (mstsc:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
It seems like powershell doesn't recognize cmdkey and mstsc. I've tried googling for hours with no solution. Hoping some one knows what the issue is here preventing the script from running.
Thanks!
Make sure that the actual executables are in your path.
e.g.
dir "$($env:systemroot)\system32\cmdkey.exe"
dir "$($env:systemroot)\system32\mstsc.exe"
The errors are specif.
cmdkey : The term 'cmdkey' is not recognized as the name of a cmdlet
mstsc : The term 'mstsc' is not recognized as the name of a cmdlet
Those two items are external executables of the Windows and ran via cmd.exe, not PowerShell. Running executables/external commands in PowerShell is a well-documented thing, directly from Microsoft.
PowerShell only runs, '.ps' files. Executables are executed via cmd.exe, even from a PowerShell script, which will call cmd.exe under the covers to run them.
If you want to see your call stack of your command, that is why the Trace-Command cmdlet exists.
# Get specifics for a module, cmdlet, or function
(Get-Command -Name Trace-Command).Parameters
(Get-Command -Name Trace-Command).Parameters.Keys
Get-help -Name Trace-Command -Examples
Get-help -Name Trace-Command -Full
Get-help -Name Trace-Command -Online
PowerShell: Running Executables
https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
As is PowerShell command precedence.
about_Command_Precedence
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_command_precedence?view=powershell-7
1 - Alias
2 - Function
3 - Cmdlet
4 - External executable files (programs and non-PowerShell scripts)
With your use case, the call operator is most prudent.
The Call Operator &
Why: Used to treat a string as a SINGLE command. Useful for dealing with spaces.
In PowerShell V2.0, if you are running 7z.exe (7-Zip.exe) or another command that starts with a number, you have to use the command invocation operator &.
The PowerShell V3.0 parser does it now smarter, in this case, you don’t need the & anymore. >
Details: Runs a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command, it cannot interpret command parameters
Example:
& 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\videos\my home video.avi" /fullscreen
Things can get tricky when an external command has a lot of parameters or there are spaces in the arguments or paths!
With spaces, you have to nest Quotation marks and the result is not always clear!
In this case it is better to separate everything like so:
$CMD = 'SuperApp.exe'
$arg1 = 'filename1'
$arg2 = '-someswitch'
$arg3 = 'C:\documents and settings\user\desktop\some other file.txt'
$arg4 = '-yetanotherswitch'
& $CMD $arg1 $arg2 $arg3 $arg4
or same like that:
$AllArgs = #('filename1', '-someswitch', 'C:\documents and settings\user\desktop\some other file.txt', '-yetanotherswitch')
& 'SuperApp.exe' $AllArgs
If the executable is not in your System path, or $env:PSModulePath, then you must provide the full path to the executable, as noted by the answer provided by 'Dan'.

How to run Exchange powershell Commands through windows command prompt?

I am trying to run Exchange PowerShell Commands through windows Command Prompt.
I searched for the same and found
powershell.exe -command Get-mailbox
which results into the following error
The term 'get-mailbox' is not recognized as the name of a cmdlet, function, scr
ipt file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:12
+ get-mailbox <<<<
+ CategoryInfo : ObjectNotFound: (get-mailbox:String) [], Command
NotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I found one more way that is creating a .ps1 file.so I created a .ps1 file named A.ps1 having one System command(Copy that is running successfully) and one exchange command
it again results into the same like above error.
note: I am running cmd as an elevated user only.
I don't know where i am going wrong.
I also found many other commands but each results into the same error (command not recognized as cmdlet..)
Any help or suggestions would be greatly appreciated
Thanks!
The exchange commands don't work in a normal Powershell console, you need to first establish a connection to your Exchange Server. The code example in the linked TechNet article will ask the user to input the credentials to connect to the exchange server. If you would like to "save" the credentials I would recommend to encrypt your credentials and don't save it in clear text in your script! For an example see this two part Blog article from PDQ.

While running a batch file from Windows 10 cmd I'm getting an error that a term is not recognised as cmdlet function

This is the exact error on the cmd window.
this is the command I typed to be executed in the cmd
F:\Fast R-CNN\Cognitive tool kit\cntk\Scripts\install\windows>install.bat
CNTK Binary Install Script
F:\Fast : The term 'F:\Fast' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ F:\Fast R-CNN\Cognitive tool kit\cntk\Scripts\install\windows\ps\inst ...
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (F:\Fast:String) [], CommandNotF
oundException
+ FullyQualifiedErrorId : CommandNotFoundException
Error during install operation
I've tried running as admin, direct clicking, changing the path etc. Kindly tell me a way to run this file. It's a batch file for a series of installations for the Microsoft cognitive tool kit.
Looks like you need to enclose the path in quotes
"F:\Fast R-CNN\Cognitive tool kit\cntk\Scripts\install\windows\install.bat"
use "start" before the filename.
Example:
F:\Fast R-CNN\Cognitive tool kit\cntk\Scripts\install\windows>start install.bat

Batch programming a runas utility creater

I'm attempting to write a batch file that makes it easier for me to make runas utility shortcuts on a computers desktop.
I have this mostly done, I have all the variables generated, I'm just having issues with the actual shortcut creation part of the script.
This is the code I am using to create the shortcut. With this code, I am using for variables: %shortcutName% as Internet Explorer, %computername% is my computers name, which doesn't include any special characters or spaces, %user% is a local user account which is an administrator (Again no special characters or spaces), and %UserInputpath% is equal to "C:\Program Files\Internet Explorer\iexplore.exe" (When you drag and drop a file into the command prompt window it generates this link, and wraps it in quotes if needed)
powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%USERPROFILE%\Desktop\%shortcutName%.lnk');$s.TargetPath='runas /user:%computername%\%user% /savecred %UserInputPath%';$s.Save()"
I think that my problem stems from the quotes as I said earlier, but I'm not really sure how to handle the issue.
This is the error that I get:
Value does not fall within the expected range.
At line:1 char:98
+ ... lorer.lnk');$s.TargetPath='runas /user:iamgroot\admin /savecred C ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
Unable to save shortcut "C:\Users\user\Desktop\Internet Explorer.lnk".
At line:1 char:203
+ ... /savecred C:\Program Files\Internet Explorer\iexplore.exe';$s.Save()
+ ~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], DirectoryNotFoundException
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoundException
My suggestion is you wrap your script with this...
#Run as Adminstrator
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
%Your Coder HERE%
}
This little section of code resolved all my not administrator errors.
Let me know how it works out for you,
The UnderDog

Powershell - rename folder in C:\Windows\

I need some help. I need to rename a few folder in c:\Windows\ but it keeps saying that access is denied. I'm running poweshell as admin.
Rename-Item -path 'C:\Windows\SoftwareDistribution' -NewName 'C:\Windows\SoftwareDistribution.bak'
I get the return:
Rename-Item : Access to the path 'C:\Windows\SoftwareDistribution' is denied.
At line:1 char:2
+ Rename-Item -path 'C:\Windows\SoftwareDistribution' -NewName 'C:\Win ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Windows\SoftwareDistribution:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
I would guess that you are running this cmdlet as consent admin. (UAC)
You have to start your powershell console as administrator.
(It is not enough to be logged in as administrator.)
I had the exact same issue:
Rename-Item -Path PATH1 -NewName PATH2
gave me an IOException. I tried the older REN command but it is aliased to Rename-Item anyway and also did not work.
The only solution that worked for me was to rename it in File Explorer.
You'd need to close your file explorer and also the IDE file then you can use Rename-Item PATH1 -NewName PATH2
One possibility is simply a permission problem, which we can rule out, apparently.
The other likely explanation is that a process still holds an open handle on that folder, or a file within, most likely Windows Update. You'd have to stop the Windows Update service before you do that.
Try using the -Force switch as well, As long as the PowerShell Session you are running the cmdlet in there shouldn't be any issues.
I think the significant part of the error is where it says IOException:
+ CategoryInfo : WriteError: (C:\Windows\SoftwareDistribution:String) [Rename-Item], IOException
Check that the folder is not in use, just in case.
I've had the same problem before. You need to make sure the directory is not open in any command prompts and you need to turn of the wuauserv service.
You can also use Svish answer in this thread to look for folder locks in the Windows Resource Monitor.
https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows

Resources