The following PowerShell script runs an ftp command but returns an error as it cannot get find the absolute source path (which includes white space and does take the complete path as "File One” and “Destination One".
Output:
/usr/bin/lftp -c \"mirror --only-missing --verbose sftp://jason#10.1.1.1:/e:/Files/File One e:/Files/Destination One/File One\"
Access failed: No such file (e:/Files/File)
The values of each variables are hardcoded and cannot be changed for a reason. In that case, what are the changes required in the $ftppath or $ftpcmd to include these white spaces in the source and dest, so it can return the ftpcmd correctly. Could someone please help?
Script:
# these values are hardcoded and cannot be modified
$root = "/e:/Files"
$source = "File One"
$dest = "e:/Files/Destination One/File One"
$user = "jason"
$server = "10.1.1.1"
# ftp command
$ftppath = "sftp://$user" + '#' + $server + ':' + $root + '/' + $source + ' ' + $dest
$ftpcmd = '/usr/bin/lftp -c \"mirror --only-missing --verbose ' + "$ftppath"+'\"'
$ftpcmd
Invoke-Expression -command $ftpcmd
so i guess you can simply use it like thi:
# these values are hardcoded and cannot be modified
$root = "/e:/Files"
$source = "File One"
$dest = "e:/Files/Destination One/File One"
$user = "jason"
$server = "10.1.1.1"
# ftp command
$ftppath = "sftp://$user" + '#' + $server + ':' + $root + '/' + $source + ' ' + $dest
$ftpcmd = '/usr/bin/lftp -c \"mirror --only-missing --verbose ' + "$ftppath"+'\"'
$ftpcmd
Invoke-Expression -command $ftpcmd.replace(" ", "\ ")
```shell
ftp command
$ftppath = "sftp://$user" + '#' + $server + ':' + $root + '/' + $source + ' ' + $dest
```
replace by this,use quote wrap $dest
```shell
ftp command
$ftppath = "sftp://$user" + '#' + $server + ':' + $root + '/' + $source + ' ' + "${dest}"
```
Related
I'm trying to execute a process from a power shell script:
$covFiles = Get-ChildItem -Name -Path .\ -Include Cov*.xml
for ($i=0; $i -lt $covFiles.Count; $i++) {
$covFile = $covFiles[$i]
$idx = $covFile.IndexOf("_")
$covPlatform = $covFile.SubString(3, 3)
$covFilter = $covPlatform + "_" + $covFile.SubString($idx + 1).TrimEnd(".xml")
$command = "-f " + $covFile + " -F " + $covFilter
& codecov $command
}
The working directory has files named, for instance:
- CovEGLOpenGL.Net.Test_net461_x86_Release.xml
- CovWGLOpenGL.Net.CoreUI.Test_net461_x64_Release...
If I echo the command with & echo $command it works as expected on my local machine.
But on AppVeyor, the process outputs:
codecov.exe : The filename, directory name, or volume label syntax is incorrect.
At line:8 char:3
+ & codecov $command
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The filename, d...x is incorrect.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
FIND: Parameter format not correct
This leads me to think that there's something wrong with the parameters specification. May it be quoting?
Replacing codecov with echo, locally outputs:
-f CovEGLOpenGL.Net.CoreUI.Test_net461_x64_Release.xml -F EGL_net461_x64_Release
-f CovEGLOpenGL.Net.CoreUI.Test_net461_x86_Release.xml -F EGL_net461_x86_Release
-f CovEGLOpenGL.Net.Test_net35_x64_Release.xml -F EGL_net35_x64_Release
-f CovEGLOpenGL.Net.Test_net35_x86_Release.xml -F EGL_net35_x86_Release
-f CovEGLOpenGL.Net.Test_net461_x64_Release.xml -F EGL_net461_x64_Release
-f CovEGLOpenGL.Net.Test_net461_x86_Release.xml -F EGL_net461_x86_Release
-f CovWGLOpenGL.Net.CoreUI.Test_net461_AnyCPU_Release.xml -F WGL_net461_AnyCPU_Release
-f CovWGLOpenGL.Net.CoreUI.Test_net461_x64_Release.xml -F WGL_net461_x64_Release
-f CovWGLOpenGL.Net.CoreUI.Test_net461_x86_Release.xml -F WGL_net461_x86_Release
-f CovWGLOpenGL.Net.Test_net35_AnyCPU_Release.xml -F WGL_net35_AnyCPU_Release
-f CovWGLOpenGL.Net.Test_net35_x64_Release.xml -F WGL_net35_x64_Release
-f CovWGLOpenGL.Net.Test_net35_x86_Release.xml -F WGL_net35_x86_Release
-f CovWGLOpenGL.Net.Test_net461_AnyCPU_Release.xml -F WGL_net461_AnyCPU_Release
-f CovWGLOpenGL.Net.Test_net461_x64_Release.xml -F WGL_net461_x64_Release
-f CovWGLOpenGL.Net.Test_net461_x86_Release.xml -F WGL_net461_x86_Release
This should fix the problem you're running into:
$covFiles = Get-ChildItem -Filter cov*.xml
ForEach ($File in $covFiles)
{
$Path = $File.FullName
$idx = $Path.IndexOf("_")
$Platform = $Path.Substring(3, 3)
$Filter = "${Platform}_$($Path.Substring($idx+1).TrimEnd('.xml'))"
$ArgList = #(
'-f',"`"$Path`""
'-F',"`"$Filter`""
)
Start-Process -FilePath codecov -ArgumentList $ArgList
}
I suspect the problem you are running into is due to relative paths.
I am trying to add 100 user accounts with PowerShell in my domain system in Windows. I get an error message called "You cannot call a method on a null-valued expression" when running the script for import.
Error Message:
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Downloads\user-import.ps1:37 char:9
+ $brukernavn = ($fornavn.Substring(0,3) + $etternavn.Substring ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull*
The PowerShell Script:
Import-Module ActiveDirectory
$csvpath = $PSScriptRoot + "\produksjonsbrukereImport.csv"
if (Test-Path $csvpath) {
#$csvpath = "C:\script\produksjonsbrukereImport.csv"
$csv = Import-Csv -Delimiter "," -Path $csvpath -Encoding UTF7
$OUBasePath = ",OU=Produksjon,OU=OpMeis,DC=OpMeis,DC=local"
$logpath = "$PSScriptRoot\import-brukere-loggfil.txt"
}
foreach ($line in $csv) {
$fornavn = $line.fornavn
$etternavn = $line.etternavn
$navn = ($fornavn + " " + $etternavn)
$beskrivelse = $line.beskrivelse
$passord = $line.Passord
$avdeling = $line.avdeling
$brukernavn = ($fornavn.Substring(0,3) + $etternavn.Substring(0,3)).ToLower()
$brukernavn = $brukernavn -replace "æ", "a"
$brukernavn = $brukernavn -replace "å", "a"
$brukernavn = $brukernavn -replace "ø", "o"
$principal = $brukernavn + "#OpMeis.local"
$profPath = ($profBasePath + $brukernavn)
$profPathTrue = $profPath + ".V2"
$OU = ("OU=" + $avdeling + $OUBasePath)
}
Here is my for loop
function Generate_bin($input_1, $bin_file_name, $Codes){
$counter=1
foreach ($Code in $Codes)
{
Write-Host "Code = $Code"
$input_11 = "$input_1" + "$counter"
$pattern = "0x"
$New = ""
$Code = [regex]::replace($Code, $pattern, "$New")
$bin_file_name2 = "$bin_file_name"
$bin_file_name2 += "$Code" + ".bin"
#This utility generates "out.bin"
Invoke-Command -ScriptBlock {.\xyz.exe -i -t "$input_1"}
Rename-Item out.bin -NewName $bin_file_name2
$counter++
}
}
I am getting following error
Rename-Item : Cannot create a file when that file already exists.
At C:\script\myParser_unique.ps1:18 char:7
+ Rename-Item out.bin -NewName $bin_file_name2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\v-ashi...Unified\out.bin:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
You should add -Force parameter to the Rename-Item to make it overwrite existing files.
If your code happens to generate multiple equal file names you could add a counter to the filename.
I am having issues with the snippet below with Powershell 4.0 on 2012 R2:
$query = 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' +
$disk.DeviceID.replace('\','\\') +
'"} WHERE AssocClass=Win32_DiskDriveToDiskPartition'
On my Windows 7 laptop running Powershell 2.0 this works fine both in the Powershell cli and in a .ps1 script. In Powershell 4.0 if I run it in the cli it works fine, but if I run it out of a .ps1 script I get:
You cannot call a method on a null-valued expression.
At C:\temp\perf.ps1:94 char:1
+ $part_query = 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' + $disk.DeviceID.rep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ( [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvokeMethodOnNull
After some debug I think the issue is with .replace('\\','\\\').
I have tried .replace('\\\','\\\\\\\') but I get the same error.
Full code:
$diskdrives = get-wmiobject Win32_DiskDrive | sort Index
foreach ( $disk in $diskdrives ) {
write $disk
$scsi_details = 'SCSI ' + $disk.SCSIBus + ':' +
$disk.SCSILogicalUnit + ':' +
$disk.SCSIPort + ':' +
$disk.SCSITargetID
#$part_query = 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' + $disk.DeviceID.replace('\','\\') + '"} WHERE AssocClass=Win32_DiskDriveToDiskPartition'
$part_query = 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' + $disk.DeviceID.replace('\\','\\\') + '"} WHERE AssocClass=Win32_DiskDriveToDiskPartition'
$partitions = #( get-wmiobject -query $part_query |
sort StartingOffset )
foreach ($partition in $partitions) {
$vol_query = 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID="' +
$partition.DeviceID +
'"} WHERE AssocClass=Win32_LogicalDiskToPartition'
$volumes = #(get-wmiobject -query $vol_query)
foreach ( $volume in $volumes) {
$allDiskInfo += $($volume.name + ' ' +
$volume.FileSystem + ' ' + $disk.Index + ' '
) | Out-String -stream
} # end foreach vol
} # end foreach part
} # end foreach disk
When I declared $disks I made it [string]. What I think happened is that Powershell 2.0 coerced it back to the system object but Powershell 4.0 must have stronger typing rules and was not coercing it from what I declared it as. Once I took [string] out of the declaration, it works on 4.0.
Thanks for the responses!!
I have a script that requires 3 mandatory input parameters that will be used to run the backup command for tdpsql.
type (FULL, DIFF or LOG)
SQLServerinstancename
Database (SYSTEM, ALL, )
Within the powershell script I have the following line
$cmd = "C:\Progra~1\Tivoli\TSM\TDPSql\tdpsqlc.exe backup " + $idatabase + " " + $action + " " + $parameter + " /LOGFILE=" + $logdir + $logfile + "" $tdpsqlexe - The tdpsqlc exe.
$idatabase - Database name
$action = FULL\DIFF\LOG
$parameter = /sqlserver=TCP:" + $sqlserverinstance + " /SQLAUTH=INT /TSMOPTFile='" + $dsmoptfilename + "' /EXCLUDEDB=" + $exclude
& $cmd
When I echo the command it reports out what I use to run it using powershell command line but when I try to run it from Powershell with the & it fails with the following
The term
C:\Progra~1\Tivoli\TSM\TDPSql\tdpsqlc.exe
backup master FULL
/sqlserver=TCP:
/SQLAUT H=INT
/TSMOPTFile=C:\Progra~1\Tivoli\TSM\TDPSql\dsm.opt /EXCLUDEDB=tempdb /LOGFILE=<logfile>
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 TDPSQLBackup.ps1:166
char:6
+ & <<<< $cmd >> test2.txt
+ CategoryInfo : ObjectNotFound:
(C:\Progra~1\Tiv...forsqlimran.txt:String)
[], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Any help will be really appreciated.
You might try Invoke-Expression $cmd instead of & $cmd.
Yes better use Invoke-Expression, but if you still want to use & you can do it in this way.
$cmd = "C:\Windows\System32\notepad.exe"
$params = "C:\temp\file.txt"
& $cmd $params
Using a var for program file ans a var for parameters.
Instead of running a batch command you'd better try Powershell cmdlets as follow
import-module "C:\Program Files\Tivoli\Flashcopymanager\fmmodulemmc.dll"
import-module "C:\Program Files\Tivoli\Flashcopymanager\fmmoduleSQL.dll"
$startTime = get-date
Backup-DpSqlComponent -Name AdventureWorks2012 -BackupDestination TSM -BackupMethod Legacy -Full
$endTime = get-date
$activity = Get-FcmMmcActivity -StartTime $startTime -EndTime $endTime
$activity
Reference link
http://www-01.ibm.com/support/docview.wss?uid=swg21974345