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
Related
I am trying to run the following powershell command from a .bat script
[void] [reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing")
$notify = new-object system.windows.forms.notifyicon
$notify.icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(10,"Copied!","Map names have been copied to the clipboard",[system.windows.forms.tooltipicon]::None)
the code is intended to produce a simple balloon notification in the bottom right hand corner of the screen
I have tested the code in powershell and it works just fine there, however i cant get it to work in cmd / via a .bat script
i dont want to have to point to a seperate powershell file, i need it all to run from within this one script
I have realised that i need to address that this is a powershell script at the beginning of the script so that it runs correctly so i have modified it to the following:
powershell -Command "& {[void] [reflection.assembly]::loadwithpartialname("System.Windows.Forms"); [reflection.assembly]::loadwithpartialname("System.Drawing"); $notify = new-object system.windows.forms.notifyicon; $notify.icon = [System.Drawing.SystemIcons]::Information; $notify.visible = $true; $notify.showballoontip(10,"Copied!","Map names have been copied to the clipboard",[system.windows.forms.tooltipicon]::None);}"
from everything i have read on here for people with similar problems this should work, i can get cmd to run other simple powershell commands which appear to work just fine, such as
powershell -Command "& { Get-Process }"
so what am i doing wrong here?
when i run the above code i get the following error message on cmd
At line:1 char:54
+ & {[void] [reflection.assembly]::loadwithpartialname(System.Windows.F ...
+ ~
Missing ')' in method call.
At line:1 char:54
+ ... eflection.assembly]::loadwithpartialname(System.Windows.Forms); [refl ...
+ ~~~~~~~~~~~~~~~~~~~~
Unexpected token 'System.Windows.Forms' in expression or statement.
At line:1 char:3
+ & {[void] [reflection.assembly]::loadwithpartialname(System.Windows.F ...
+ ~
Missing closing '}' in statement block or type definition.
At line:1 char:74
+ ... flection.assembly]::loadwithpartialname(System.Windows.Forms); [refle ...
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:120
+ ... m.Windows.Forms); [reflection.assembly]::loadwithpartialname(System.D ...
+ ~
Missing ')' in method call.
At line:1 char:120
+ ... s); [reflection.assembly]::loadwithpartialname(System.Drawing); $noti ...
+ ~~~~~~~~~~~~~~
Unexpected token 'System.Drawing' in expression or statement.
At line:1 char:134
+ ... ); [reflection.assembly]::loadwithpartialname(System.Drawing); $notif ...
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:300
+ ... ormation; $notify.visible = $true; $notify.showballoontip(10,Copied!, ...
+ ~
Missing expression after ','.
At line:1 char:300
+ ... n; $notify.visible = $true; $notify.showballoontip(10,Copied!,Map nam ...
+ ~~~~~~~
Unexpected token 'Copied!' in expression or statement.
At line:1 char:307
+ ... ; $notify.visible = $true; $notify.showballoontip(10,Copied!,Map name ...
+ ~
Missing argument in parameter list.
Not all parse errors were reported. Correct the reported errors and try again.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
is it something to do with the syntax i am using? maybe to do with spaces or something?
You can embed your PowerShell script and execute it from a batch file with this hybrid method, and you will don't worry about any escape special character anymore :
<# : Batch Script Section
#rem # The previous line does nothing in Batch, but begins a multiline comment block in PowerShell. This allows a single script to be executed by both interpreters.
#echo off
Title Embed And Execute Powershell Script with a Batch file in Hybrid Mode & Mode 70,3
cd "%~dp0"
Color 1B & echo( & Echo(
Echo( Please Wait ... Loading PowerShell script is in progress ...
Powershell -executionpolicy bypass -Command "Invoke-Expression $([System.IO.File]::ReadAllText('%~f0'))"
EndLocal
pause
goto:eof
#>
# Powershell Script Section begin here...
# Here we execute our powershell commands...
[void] [reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$notify = new-object system.windows.forms.notifyicon
$notify.icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(10,"Copied!","Map names have been copied to the clipboard",[system.windows.forms.tooltipicon]::None)
Here is an example that I used this hybrid method : SpeedTest_Hackoo_Ookla.bat
I have the following inside my Power Shell PS1 file, to set Tls12 + call .exe :-
Show-Message -Message "Step 1a: Create groups and adding users to it"
& "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $HelperPath\UpdateView.exe" "true" $Username $Password
If ((Get-Content $ErrorLogFile) -ne $Null) {
Show-Message -Message "Creating group and adding users to it failed" -Type ([MessageType]::Failure)
RevertAll $ScriptDirectory 1
return
}
but i am getting this error:-
& : The term '[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
\UpdateView.exe' 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:\c\tree\master\cloud\src\deployments\Scripts\Deploy.ps1:352 char:7
+ & "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityPro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ([Net.ServicePoi....UpdateView.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
any advice please?
&, the call operator takes a command / executable name / path only as an argument, not whole statements.
(While it can also accept a script block { ... } containing one or more full statements, there's no need for it here, though you can generally use it to create a child scope).
Simply execute the two statements in sequence:
# Set the protocol (an assignment statement)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Call the executable (a command call).
& $HelperPath\UpdateView.exe" true $Username $Password
What am I doing wrong here? I am trying to open a second PS1 file if True but an error is thrown (shown below).
#ABOUT
#GLOBALS
$userID = "admin"
$pswrd = "test"
$name = Read-Host 'What is your username?'
$pass = Read-Host 'And your password?' #-AsSecureString | ConvertFrom-SecureString
#$script = '.\sdsSysMain.ps1'
if($name -eq $userID -and $pass -eq $pswrd) #or blank?
{
#write-host "Well done! You're in. "
#Start-Sleep -s 5
Powershell -noexit ".\sdsSysMain.ps1"
}
elseif($name -ne $userID -or $pass -ne $pswrd)
{
write-host "Login Failed... :("
}
This is the error I'm getting:
powershell.exe : The term '.\sdsSysMain.ps1' is not recognized as the name of a cmdlet, function
At C:\Users\1234\Documents\Projects\sdsSys\sdsSysLogin.ps1:19 char:15
+ Powershell <<<< -noexit ".\sdsSysMain.ps1"
+ CategoryInfo : NotSpecified: (The term '.\sds...mdlet, function:String) [],
RemoteException
+ FullyQualifiedErrorId : NativeCommandError
, 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:17
+ .\sdsSysMain.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (.\sdsSysMain.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The error is rather self-explanatory. The script doesn't find sdsSysMain.ps1 in the current working directory. You can output the current working directory in your script like this:
(Get-Location).Path
If you want to run sdsSysMain.ps1 from the same directory as the script calling it, change this:
Powershell -noexit ".\sdsSysMain.ps1"
into this:
$scriptPath = Split-Path -Parent $script:MyInvocation.MyCommand.Path
Powershell -NoExit -File "$scriptPath\sdsSysMain.ps1"
I am trying to execute an installation via start-process but i want it to execute as a job so i can execute a few other statements while also being able to check the status of the installation as it runs in the background.
Here is a section of the code i am trying to execute -
$SetupPath = "C:\Test Installs"
# Enclose the path to setup.exe in quotes
$Setup = "`"" + "$SetupPath\setup.exe" + "`""
$command = "{$SetupProcess=" + "Start-process" + " " + `
"$Setup" + " "+ "-ArgumentList" + " " + `
"/config config.xml" + " " + "-Wait -PassThru" + "}"
# The above command equals-> {$SetupProcess=Start-process "C:\Test Installs\setup.exe" -ArgumentList /config config.xml -Wait -PassThru}
#Change string to Scriptblock
$ScriptBlock = [Scriptblock]::Create($command)
$job1 = Start-Job -ScriptBlock $ScriptBlock -Name "SetupJob"
When I run this and try to get back the result via Receieve-Job i only get back the command string I passed via script block. It appears the Start-process command is not executing. Is there something i am missing?
Get rid of the {} in your $command definition. [ScriptBlock]::Create() expects some text in which it will wrap in a scriptblock. You can also simplify this:
$SetupPath = "C:\Test Installs"
# Enclose the path to setup.exe in quotes
$Setup = "`"$SetupPath\setup.exe`""
$command = "SetupProcess = Start-process `"$Setup`" -ArgumentList `"/config config.xml`" -Wait -PassThru"
I'm using invoke-expression in PowerShell to create an archive, but it's not working due to spaces in the exe path. These are my variables:
Set-Variable -name DIRRELEASE -value (Join-Path $env:UserProfile "\Documents\Coding\_Projects\ChickenPing\trunk\Dist\current\")
$srcPath = (Join-Path $DIRRELEASE ("myapp_1.90-src.zip"))
Set-Variable -name WinRarFilter -value "-x*\.svn -x*\.svn\* -x*\nutrition.db3"
Set-Variable -name WinRarOpts -value "-r -s -m5 -inul"
$WinRar = `"C:\Program Files\Winrar\winrar.exe`"
#Then using this to invoke:
Invoke-Expression ($WinRAR + " a " + $srcPath + " " + $WinRARFilter + " * " + $WinRAROpts)
When I run the script I get this error:
The term 'a' 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:3
+ a <<<< C:\Users\Echilon\Documents\Coding_Projects\MyApp\trunk\Dist\c
urrent\myapp_1.95-src.zip -x*.svn -x*.svn* -x*\nutrition.db3 * -r
-s - m5 -inul
+ CategoryInfo : ObjectNotFound: (a:String) [], CommandNotFoundEx ception
+ FullyQualifiedErrorId : CommandNotFoundException
I just can't find the right combination of quotes and plusses.
You could make this a lot easier to work with by using the call operator &.
& $WinRAR a $srcPath "-x*\.svn" "-x*\.svn\*" "-x*\nutrition.db3" * -r -s -m5 -inul