I need to copy a part of a file name and make it become a variable, for example my file is called "test-123456.txt" I need the variable $ part = 123456 clearly this inserted in a loop where the part of the file name "test" is always the same but the part "123456" changes.
CodePudding user response:
Try using below as a minimal example to play with but my also solve your problem.
$files = @('C:\fileName_12345.txt','C:\fileName_54321.txt')
foreach ($file in $files) {
$varName = $file.split("/[ \w-] \./g")[1]
Write-Host "'varName' is the variabled named $varName...use it well"
}
CodePudding user response:
This my part of script to better understand
$complist = Get-Content "F:\Source\complist3.txt"
foreach($comp in $complist)
{
if (Test-Connection -ComputerName $comp -Quiet -Count 1 -ErrorAction SilentlyContinue)
{
Remove-Item -Path "\\$comp\c$\Users\use1\AppData\Local\Programs\Update\*.zip" -Force
Move-Item -Path "\\$comp\c$\Users\use1\AppData\Local\Programs\config\General123456.json" -Destination "\\$comp\c$\Users\Use1\AppData\Local\Programs\Update\General123456.json" -Force
#IN THIS POINT NEED CREATE A VARIABLE FOR COPY 123456 FROM General123456.json
New-Item "\\$comp\c$\UpdateNH\" -ItemType Directory -Force
New-Item "\\$comp\c$\UpdateNH\Source" -ItemType Directory -Force
New-Item "\\$comp\c$\UpdateNH\log" -ItemType Directory -Force
Copy-Item -Path $pathfilelocale -Destination "\\$comp\c$\UpdateNH\Source" -force
Invoke-Command -ComputerName $comp -ScriptBlock {
########FOLDER 2##
if (Test-Path $using:path2)
{
Add-Type -Path "C:\UpdateNH\Source\WinSCPnet.dll"
function FileTransferProgress
{
param($e)
Write-Progress `
-Activity "Downloading" -Status ("{0:P0} complete:" -f $e.OverallProgress) `
-PercentComplete ($e.OverallProgress * 100)
Write-Progress `
-Id 1 -Activity $e.FileName -Status ("{0:P0} complete:" -f $e.FileProgress) `
-PercentComplete ($e.FileProgress * 100)
}
####SESSIONE#####
$script:lastFileName = $Null
try
{
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = $using:ftp
UserName = "user"
Password = "psw"
}
$session = New-Object WinSCP.Session
try
{
#Report
$session.add_FileTransferProgress( { FileTransferProgress($_) } )
#Connect
$session.Open($sessionOptions)
#Download
$session.GetFiles("$using:folderagg*", "$using:targetzip2").Check()
}
finally
{
#Terminate line
if ($script:lastFileName -ne $Null)
{
Write-Host
}
#Disconnect
$session.Dispose()
}
########INSTALLAZIONE########
Expand-Archive $using:sourcezip2 -DestinationPath $using:target2 -Force
# IN THIS POINT I NEED OPEN Generl987456.json and replace a specific string with variable create before 123456
# IN THIS POINT I NEED RENAME General987456.json to General variable123456 .json
#
# All pc in complist have a different name in Generalxxxxxx.json
Start-Transcript $using:logfolder2\$using:comp.txt -Append
Write-Host "Aggiornameno effettuato il $using:date"
Stop-Transcript
shutdown -r /t 10
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
}