This question already has answers here:
Get only filename from full path of a file
(2 answers)
Closed 5 years ago.
I'm writing a PowerShell script to output the process,
$Process=chknull $_.Properties[0].Value
$Process_new=$Process -replace '(?<! .*) ','_'
The above code outputs the result as entire path of process, Eg:
C:\Windows\System32\Notepad.exe.
Is there a way, where I can get just "Notepad.exe"
If you're getting a full path return like that as a string you can set the value into a variable and then use split on it and grab the last entry in the resulting array.
Example:
("C:\Windows\System32\Notepad.exe." -split "\\")[-1]
Remember to use two "\" as it is also an escape character in regex.
([IO.FileInfo]"C:\Windows\System32\Notepad.exe").Name
Related
This question already has answers here:
Using a Variable (PowerShell) inside of a command
(2 answers)
Closed 1 year ago.
I have trouble with following piece of code:
$result = "F651F545A059588ABFDCE7EE3EEB27EED8CED28D"
winrm create winrm/config/Listener?Address=*+Transport=HTTPS '#{Hostname="my.host.winagent"; CertificateThumbprint="$result"}'
it yields:
Message = The WS-Management service cannot process the configuration request because the certificate thumbprint in the request is not a valid hex string: $result.
it looks like variable result is not being expanded, probably because it is inside array? How to get the variable value expanded there?
You need to use " (not ') to create an expandable string literal - escape the literal "'s by doubling them:
winrm create winrm/config/Listener?Address=*+Transport=HTTPS "#{Hostname=""my.host.winagent""; CertificateThumbprint=""$result""}"
This question already has answers here:
Counting characters in a specific text file
(4 answers)
Closed 4 years ago.
I have data in a file like below.
text1|text2
text3|text4|
I'm looking for a Power Shell command to count number of pipes present in thisfile.
Normally I would want you to show some effort but this is trivial enough I won't bother:
$pipeCount = (get-content file.txt -Raw).Split('|').count - 1
$pipeCount
This question already has answers here:
INI file parsing in PowerShell
(8 answers)
Closed 5 years ago.
(I've reviewed the other answers but didn't find it match my question)
I have an INI file that looks like that:
[DATA]
installerDir=C:\installerDir
scriptsDir=C:\Scripts
TargetDirName=MSI
[DB]
DB_Name="Database_2211"
DistribDir=DataDist
TargetDir=TarMSI
I want to get as an output 2 files that will look like that:
First file will contain the following data:
installerDir=C:\installerDir
scriptsDir=C:\Scripts
TargetDirName=MSI
Second file will contain the following data:
DB_Name="Database_2211"
DistribDir=DataDist
TargetDir=TarMSI
Files names should be something_DATA.ini & something_DB.ini accordingly.
Files content should not include the block name (i.e.: DATA -or- DB...)
I found that code for bash but cannot find it for PowerShell.
You could do this:
$Ini = Get-Content 'Something.ini'
$Split = $Ini -Replace '\[DATA\]' -Split '\[DB\]'
$Split[0].Trim() | Out-file 'something_DATA.ini'
$Split[1].Trim() | Out-file 'something_DB.ini'
Explanation:
Reads the contents of the .ini file with Get-Content
Replaces the [DATA] text part with blank (to remove it) and splits the content based on the [DB] part (which is also removes it)
Removes blank lines from each part of the split (accessing them with their array index [0] and [1]) and redirects them to the files.
This question already has answers here:
How do I split a string into an array by comma but ignore commas inside double quotes?
(7 answers)
Closed 8 years ago.
I want to split a csv file. ex a row contain name,id,address,pin etc..... if the adress contains again commas then how to parse? ex: Abc,123,"xyz,asd,pqr",123456 "xyz,asd,pqr" is one element and kept in double quotes only
Use a CSV parser. If you don't want to install new modules for some reason then look at Text::ParseWords instead.
This question already has answers here:
What does the '`' character do in Perl?
(3 answers)
Closed 8 years ago.
I am a beginner in perl.
Just wanted to understand the following code.
sub get_files {
foreach my $customer (keys %customers){
lg("Getting files from ftp for customer $customer", "LOG");
my $ftp_server = $customers{$customer}{'FtpServer'};
my $ftp_user = $customers{$customer}{'FtpUser'};
my $ftp_pass = $customers{$customer}{'FtpPass'};
my $datadir = $datafiles.$customer."/";
`$get_files $ftp_server $ftp_user $ftp_pass $datadir`;
}
}
What does last line in the above subroutine tells?
It invokes the command that's in the string $get_files, passing the command the remaining strings as parameters.
Usually it's used if you want to capture the resulting output and store it in a variable. In this case where the result is being discarded it would be more usual to use system instead:
system $get_files, $ftp_server, $ftp_user, $ftp_pass, $datadir;
although if the command does then produce any output it'll appear on-screen instead of being absorbed by the back-ticks operator.