cannot make sqlcmd work after runas - sql-server-2008-r2

I followed a few examples here, but I still can not get my sqlcmd work after runas.
I can make it work with two steps:
Step one: Use runas to login into a new login and open a command prompt:
runas.exe /savecred /user:DOMAIN_NAME\login_name cmd.exe
Step two: execute the sqlcmd in a script
sqlcmd -S server_name -E /Q"exit(SELECT ##version )"
But I want to make it one step to get the results. I tried to add " " after the runas command as listed below, but it did not work:
runas.exe /savecred /user:DOMAIN_NAME\login_name "sqlcmd -S server_name -E /Q"exit(SELECT ##version )""
Any ideas?

Take a look at this article describing RunAs. Toward the end of the article, he specifically addresses needing to use quotes inside of quotes:
Fortunately, it’s fairly easy to make RunAs happy. All you need to do is use the \ character to “escape” any double quote marks that must to be embedded within the process path.
So, it looks like your command should be:
runas.exe /savecred /user:DOMAIN_NAME\login_name "sqlcmd -S server_name -E /Q\"exit(SELECT ##version )\""
Notice the \ before both of the nested double quotes.

Related

How to escape # within a string in PowerShell?

I'm trying to send a HTML request via cURL in PowerShell. The code in question:
$command = 'curl -d #request.txt -o testoutput.xml -X "POST" -H #header.txt -u "username:password" "URL"'
Invoke-Expression $command
The HTML Body (-d) and Header (-H) need to be read from files, hence the # before the filenames.
In order for this to work, I need to escape the # so PowerShell doesn't interpret it as a splat operator - and that's my problem, nothing I've tried so far worked:
putting it in single quotes
here document (#" "#)
here string (#' '#)
using string templates with placeholders ("{0}request.txt" -f "#")
Unicode escape ("`u{0040}request.txt")
How do I escape the # correctly? Or am I on the wrong path entirely? (I'm new to PowerShell sorry)
Invoke-Expression (iex) should generally be avoided; definitely don't use it to invoke an external program or PowerShell script.
Therefore:
invoke curl directly
and quote PowerShell metacharacters such as # (see this answer for the full list) - either individually, with ` (e.g., `#), or by enclosing the entire argument in quotes ('...' or "...", as appropriate).
curl -d `#request.txt -o testoutput.xml -X "POST" -H #header.txt -u "username:password" "URL"
As for what you tried:
The primary concern about Invoke-Expression use is one of security: unwanted execution of (additional) commands potentially injected into the string passed to it.
In terms of functionality, Invoke-Expression is not only not needed for regular calls to external programs, it can create quoting headaches.
However, given that you're passing a '...' string to Invoke-Expression, quoting the # as `# would have worked, as shown in this simple example:
# OK, but generally do NOT do this.
Invoke-Expression 'Write-Output `#'
Had you used a "..." string, the ` would have been "eaten" by the up-front parsing of such an expandable string, based on the escaping rules explained in the conceptual about_Quoting_Rules help topic:
# BREAKS, because the ` is stripped before Invoke-Expression sees it.
Invoke-Expression "Write-Output `#"

How to use istool command in perl system() function

I am trying to put below command in perl system() function.But getting so many compilation errors (syntax).
./istool export -domain serviceshost:9080 -u dsadm -p password -ar test.isx -pre -ds '-base="ENGINEHOST/Dev_Project" Jobs/Batch/\*.*'
I was using it like in perl:
system("./istool export -domain serviceshost:9080 -u dsadm -p password -ar test.isx -pre -ds '-base="ENGINEHOST/Dev_Project" Jobs/Batch/\*.*'");
can some one guide me exactly how to use it in system function?I tried escaping . also with backslash(\) in front of it.
Replace system with print, and it's obvious you didn't build the string correctly.
If you want to include a " in a string quoted with ", you need to escape it.
If you want to include a \ in a double-quoted string, you need to escape it.

PowerShell equivalent of curl http://www.example.com/script.sh | bash -s <arguments>

How to run a remote script in PowerShell with arguments.
For linux I'm using curl http://www.example.com/script.sh | bash -s <arguments>.
Is there a similar equivalent in PowerShell.
Use Case: User-Data script for Amazon EC2
Thanks
You'll often find examples out there like this:
iex (New-Object System.Net.WebClient).DownloadString('http://domain/script.ps1')
iex is an alias for Invoke-Expression (think of it like exec or eval in some other languages).
The rest of it is creating a [System.Net.WebClient] object, which has a method to download the contents of a URL and return it as a string.

Passing value from TK to a file and replacing a variable

I am trying to interface between tk and cshell-script.
I am able to collect data using tk:
label .firstColumn.s.variable.label -text "myFirstVariable" -background $color3
entry .firstColumn.s.variable.entry -textvariable program
But when I try to run the command it does not work
button .secondColumn.o.buttons.go -text "Run Now" \
-command "exec sed -i {s/ABC/$myFirstVariable/g} runme.sh \
>! runmeNow.sh ; ./runmeNow"
It changes ABC to blank in runmeNow.sh file.
Is there any better way to achieve it?
I want to replace a place-holder predefined in cshell-script (runme.sh). My place holder is ABC. Then I want to pipe it to a different file, then run this file. runme.sh has UNIX based run file.
Seems to me that tk is interpreting $myFirstVariable as a variable of its own, while you'd like it to be forwarded to shell. Escaping the dollar sign with a backslash may not be enough: exec is a Tcl command and doesn't use a shell, so we may have to call one to expand shell variable:
button .secondColumn.o.buttons.go -text "Run Now" \
-command "exec /bin/sh -i {sed "s/ABC/$myFirstVariable/g" runme.sh \
> runmeNow.sh ; ./runmeNow.sh}"

Differences in calling CSVDE from Powershell vs Command Prompt

Can anyone explain why calling the CSVDE utility from an elevated Windows command prompt would differ than using the same string from an elevated Powershell console? The issue I have is that I can successfully export from Active Directory via the command prompt method, but Powershell returns an authentication error, "Simple bind returned 'Invalid Credentials'.
Here is the command used for both, edited for sensitive pieces:
c:\csvde.exe -s domain.company.org -f ExportFile.csv -l "givenName,sn,ipPhone,title,department,company,physicalDeliveryOfficeName,mail" -d "OU=ABC Group,OU=ABC Users,DC=DomainName,DC=org" -a UserDistinguishedName Password
Both sessions are running with Administrator privileges. I am a Powershell novice, and for the life of me, I can't figure this out.
Thanks!
I can see two potential issues. First thought is that it is taking your user name and password as separate arguments, or as Adi Inbar suggested: you've got some special characters that are causing issues. Either way I'd enclose things in single quotes so that arguments are passed as expected, and taken literally.
c:\csvde.exe -s 'domain.company.org' -f 'ExportFile.csv' -l 'givenName,sn,ipPhone,title,department,company,physicalDeliveryOfficeName,mail' -d 'OU=ABC Group,OU=ABC Users,DC=DomainName,DC=org' -a 'UserDistinguishedName' 'Password'
See if that doesn't resolve the issue. In my experience that is usually a safe way to run a executable from PowerShell. I suppose alternatively you can assign them to variables, and then pass those.
$Server = 'domain.company.org'
$OutFile = 'ExportFile.csv'
$Attributes = 'givenName,sn,ipPhone,title,department,company,physicalDeliveryOfficeName,mail'
$SearchRoot = 'OU=ABC Group,OU=ABC Users,DC=DomainName,DC=org'
$UserID = 'CN=TMTech,OU=Users,DC=Some,DC=Company,DC=org'
$Password = 'P#$$w0rd'
CSVDE.exe -s $Server -f $OutFile -l $Attributes -d $SearchRoot -a $UserID $Password