Currently, I get with this command all my mac addresses from the PowerShell:
getmac /s $computer /FO TABLE /NH
It looks like this:
PS C:\Users\User> getmac /s $computer /FO TABLE /NH
F8-5E-A0-2B-C4-5B Nicht zutreffend
94-8D-95-41-5F-57 Medien ausgeworfen
F8-5E-A0-2B-C4-5F Medien ausgeworfen
08-3A-88-5C-03-28 Medien ausgeworfen
Now, I want to combine all addresses to a string like this:
F8-5E-A0-2B-C4-5B / 94-8D-95-41-5F-57 / F8-5E-A0-2B-C4-5F / 08-3A-88-5C-03-28
I know that I must use join but I don't know how.
Can someone help me with that?
Using getmac:
(getmac /FO TABLE /NH) -ne '' -replace '\s.*$' -join ' / '
The getmac.exe program is considered external and its output is converted to an array of strings within PowerShell. As a result, you will need to parse strings for your desired result.
Since -replace uses regex, \s matches a space and .* matches all characters until $ (end of each string).
Using Get-NetAdapter:
(Get-NetAdapter).MacAddress -join ' / '
Using a PowerShell cmdlet, you return a rich object with properties that can be referenced, filtered, and parsed as needed.
Related
I need to find the string "substring1 substring2" in a datafile of a current directory.
I use the cmd of Windows-10.
I use the following command according the Windows documentation:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/findstr#examples
findstr /c:"hello there" x.y
In this way it doesn't find anything, allthough the string is present.
When I use the same instruction without ':'
findstr /c:"hello there" x.y
it shows me all cases of 'hello' and all cases of 'there'.
Here it is the concrete case:
findstr /c: "INVITE sip:" .\*5656*.dat
FINDSTR: Argoment not found after /c
Pls. help
This works fine for me, so I guess it is an issue of case sensitivity. You might need to add /i, like this:
findstr /c:"INVITE sip:" /i *5656*.dat
You mention without ':', but the command is just like the previous one (maybe you mean no colon after "sip"?). It won't work without the colon in the parameter, the colon in the string works just fine. I am not sure if findstr looks for the words separately ("it shows me all cases of 'hello' and all cases of 'there'"), I wasn't able to do that. And your final example won't work either because of the extra space after the colon.
In case this does not work at all, are the files in a subfolder? Have you tried simpler terms and then start adding words to the string? Do they have any special characters?
Update
Based on your comments, there might be a carriage return character in the outputted string, so what you can do is redirect the console output to a file, and then open that file, preferably with something better than Notepad, like Notepad++.
findstr /c:"INVITE sip:" *5656*.dat > results.txt
I was trying to execute a powershell command from ruby code.
Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='Qlik Sense Client'" |Select-Object -Property version
It gives me the version of the product perefectly. But the same thing when i try to execute from ruby like(entire command in backticks) :
find = powershell.exe Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name=''"|Select-Object -Property version
The command breaks, it cant interpret the quotes, pipes etc in ruby. I tried to escape those quotes but it still it breaks. I dont know how to escape that pipe as well.
Kindly help me here or refer me to something relevant. Many thanks.
I have tested this now:
require 'base64'
cmd = %{Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='Qlik Sense Client'"|Select-Object -Property version}
encoded_cmd = Base64.strict_encode64(cmd.encode('utf-16le'))
find = `powershell.exe -encodedCommand #{encoded_cmd}`
Powershell expects UTF-16LE-encoded strings, so you have to convert from Ruby's encoding (UTF-8) before the base64 conversion.
Alternatively you could try using shellescape from shellwords to escape the command so the shell interprets it as a single string.
Another alternative is to use powershell.exe -Command - with popen3. That will let you write your commands and read their results using a file stream.
Here an example of how I shell to external apps, should also work with Powershell. Copy the working command from your console between the {} without extra delimiters. The %Q also makes it possible to do interpolation of variables in case your command is not always the same, it works the same way as between the " delimiter.
All the output is captured by the " 2>&1" and enumerated line by line in the block after while.
In case more than one line is captured you need to check which line the result you are after is displayed and return the result.
def powershell
command = %Q{your command just like you execute it in a console}
IO.popen(command+" 2>&1") do |pipe|
pipe.sync = true
while lijn = pipe.gets
# do whatever you need with the output of the command
# return the result
end
end
end
Use the %q{} alternative if you don't need the interpolation since that also could give problems. Use the Ruby comamnd in the same console as where you get your powershell results. Ensure you can run powershell from there (must be in the path).
But as I understand this will get you the name and version of the installed products on your pc.
Why not use just Ruby ? It's way more faster than the VERY slow Wmi queries.
require 'win32/registry'
Win32::Registry::HKEY_LOCAL_MACHINE.open(
'Software\Microsoft\Windows\CurrentVersion\Uninstall'
) do |reg|
reg.each_key do |key|
k = reg.open(key)
puts key
puts k["DisplayName"] rescue "?"
puts k["DisplayVersion"] rescue "?"
puts
end
end
This is my PowerShell file:
#Replace.ps1
Param(
[string]$filepath,
[string]$find,
[string]$replace
)
$content = Get-Content $filepath
$content = $content.replace($find,$replace)
$content | out-file $filepath
This is the batch file which I am using it to call this
#ChangeIP.bat
#echo on
powershell.exe -File E:\Replace.ps1 %1 %2 %3
Now when I try to call the batch file from cmd as:
ChangeIP.bat "E:\foreign logs.txt" firstword secondword
then it is showing some ridiculous errors.
I basically am stuck in passing the file name (which is having white spaces).
The code I need basically should be able to do the following things:
A PowerShell script that takes three command line arguments:
FilePath // With white spaces (don't know how)
String to replace
String to be replaced with
The PowerShell script should be able to fetch the contents of the "FilePath" supplied. Find the "String to replace" string and Replace it with "String to be replaced with" string
Then calling this PowerShell script via batch file and supplying the three command line arguments there.
Please keep in the mind, the file path contains spaces.
Try the -replace operator instead of the Replace() method in case you're starting PowerShell v2:
$content = $content -replace $find $replace
Im trying to recursively go through a folder structure and update a bunch of pom.xml files. I want to only update my version number so I'm trying to be as exact as possible. What I want to change is:
<version>5.1.1</version>
to
<version>5.2.0</version>
Im trying to include the version tags to be sure I dont replace any comments or dependencies this same version number may appear on.
I think the characters like '<,> or /' are causing issues.
I don't have much experience with escaping characters like this on the command line so any help is appreciated.
I am on a Windows 7 machine but have Git Bash and Cygwin installed.
I am either using a tool called "fart.exe" for this - if the replacement is simple. https://sourceforge.net/projects/fart-it/
If I need regex I use power shell.
Here is an example (mix of batch-file and power-shell) which replaces a version string in all XML files:
[replace.bat]:
SET version=1.2.3
for /r %%x in (*.xml) do (
powershell -Command "& {(Get-Content '%%x') | Foreach-Object { $_ -replace '(''version''\s?\:\s?'')(\d*\.\d*\.\d*)('')', '${1}%version%${3}' } | Set-Content '%%x'}"
)
In a DOS script, if I have a variable string, how do I get a true or false that a certain string exists within that variable string? ( I don't want to have to create a temp file to accomplish this. I know how to do that hack already.) FIND.exe and FINDSTR.exe both seem to require a physical file rather than a variable.
I tried this, but it fails:
C:\Users\me>findstr.exe "Program" %ProgramData%
If you are trying to use a stock windows install - I don't think this can be accomplished as you describe using CMD.EXE as the closest you will get would be with the IF command, but it doesn't support a contains, it only contains the following operators:
EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal
If you can get away with what you want to do using those operators (and not strictly just a contains). You can also use the FOR command to split your string and test your matches on each individual element, though that seems rather brittle.
Since you have tagged this as PowerShell, I am going to go out on a limb and assume that a PowerShell script solution would be acceptable as well. If that is the case, then the solution is quite simple to demonstrate:
# create a variable $s with some string data
$s = "Some random program data"
# using String::Contains
if ($s.Contains("random")) { "Bingo" }
# using -like operator
if ($s -like "*random*") { "Bingo" }
# using the -match operator
if ($s -match "random") { "Bingo" }
Note: In PowerShell, the -contains operator is used to test for set membership, not substring matching. You will want to use the Contains method on a string object or the -like or -match operators to perform the desired patter matching.
Found another idea in the DOSTIPS forum:
set "str=-debug -verbose -normi -homedir -repo"
if "%str:-verbose=%" neq "%str%" (echo -verbose found) else (echo -verbose not found)
Instead of
C:\Users\me>findstr.exe "Program" %ProgramData%
try
echo %ProgramData% | findstr Program >nul
set RESULT=%ERRORLEVEL%
If it matches, %ERRORLEVEL% will be 0, otherwise, 1.