powershell

Question about command execution in powershell

Can someone help me explain what is happening here? Sorry if this is a basic question, I simplified it from a pipeline expression I'm trying to write: $foo = pwd $cmd = "dir" & $cmd $foo #Works dir $foo #Works & "dir $foo" #Error *The term 'dir C:\' is not recognized as a cmdlet, function, operable program, or script file. Verify ...

doing textwrap and dedent in Windows Powershell (or dotNet aka .net)

Background Python has "textwrap" and "dedent" functions. They do pretty much what you expect to any string you supply. textwrap.wrap(text[, width[, ...]]) Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines. textwrap.dedent(tex...

Powershell: Setting an environment variable for a single command only

On Linux, I can do: $ FOO=BAR ./myscript to call "myscript" with the environment variable FOO being set. Is something similar possible in Powershell, i.e. without having to first set the variable, call the command, and then unset the variable again? To be more clear about my use case - I don't want to use this as part of a script. R...

Error when redirecting stdout and stderr of powershell script

The script runs fine when stdout/stderr are not redirected. When I add both stderr and stdout redirection, I getg the following error: How can I avoid it? % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 19.4M 0 0 ...

Powershell V2 External MAML Help

I'm trying to create an external MAML help file for a script Module. As a test I created a simple module called "ModTest" with 2 functions saved in a .psm1 file: function Test-SqlScript2 { } function Out-SqlScript2 { } I saved the module in my user Modules directory ~\Documents\Modules\ModTest Next I created a subdirectory for a MAML...

How to retrieve new row data from INSERT using Oracle DataAccess with Powershell?

I am using Oracle.DataAccess.Client inside Powershell. What I need to do is INSERT a new row of data, then retrieve the auto-generated ID field of the newly-created row for another INSERT command, immediately following. What is the best way to do this? I am pretty new to SQL and Oracle. Here is some of my code: $conn = "My Connection St...

powershell history: how do you prevent duplicate commands

Background: Powershell history is a lot more useful to me now that I have a way to save history across sessions. // run this every time right before you exit powershell get-history -Count $MaximumHistoryCount | export-clixml $IniFileCmdHistory; Now, I'm trying to prevent bash powershell from saving duplicate commands to my history. ...

How to add new DNS zone on Windows Server 2008 using API or PowerShell?

Is there any API in Microsoft DNS Server 2008 to add new forward DNS using VBScript or PowerShell or P/Invoke or WMI? It's easy to generate plain text file and put it into \system32\dns. How to replace a number manual commands in MMC with just a script execution? ...

Powershell get-eventlog message column is too short

When using powershell to retrieve info about events Message column gets trimmed and is too short: Index Time Type Source EventID Message ----- ---- ---- ------ ------- ------- 2 Sep 18 12:50 Info yaddayadda 0 Class: Controllers.BasketController... 1 Sep 18 12:...

Execute javascript trough Internet Explorer's com interface using PowerShell

I am writing some Internet Explorer automation scripts using PowerShell. Here is how I start the IE com object: $ie = New-Object -com "InternetExplorer.Application" $ie.Navigate("about:blank") $ie.visible = $true $doc = $ie.Document So, what I would like to do is to execute some javascript on the $doc object. For example, I have an i...

PowerShell 1 is not capturing batch file output with tee

PowerShell can call commandline batch files. PowerShell script output can be recorded with the "tee" command. But the tee command does not record the output of batch files inside a PowerShell script for me in PowerShell 1. Try this cut-down example: Make a batch file, called test.bat, with contents @echo hello from bat Run it from P...

Powershell errors: Is there a way to trap "command not found" errors into a custom function?

Background: Suppose I have a powershell function that understands and interprets small_commands such as: todolist.additem/title=shopping/body=get milk Suppose further that I invoke the function like this: myfunc [small_command goes here] Question: Is there a way that I can type in the small_command and still have powershell invoke...

How to know if PowerShell script is being run with RunspaceInvoke.

I have a PowerShell script that needs to run normally, and also when invoked via RunspaceInvoke, e.g. using (RunspaceInvoke invoker = new RunspaceInvoke()) { invoker.Invoke(powerShellScript); } Part of this script doesn't need to run when being called with RunspaceInvoke, and moreover, it fails when being called with RunspaceInvok...

Can I get return value prepared by powershell before returning from function?

Is it possible to know the return value prepared by powershell before returning from function? Pseudo code: function Foo() { 1 2 Write-Host $CURRENT_RESULT # I would like it to print [1, 2] 3 4 Write-Host $CURRENT_RESULT # I would like it to print [1, 2, 3, 4] } ...

Create Outlook email draft using PowerShell

I'm creating a PowerShell script to automate a process at work. This process requires an email to be filled in and sent to someone else. The email will always roughly follow the same sort of template however it will probably never be the same every time so I want to create an email draft in Outlook and open the email window so the extr...

Learning... anything really

I'm particularly interested in Windows PowerShell, but here's a somewhat more general complaint: When asking for help on learning something new, be it a small subject on PHP or understanding a class in Java, what usually happens is that people direct me towards the documentation pages. What I'm looking for is somewhat of a course. A d...

Reload display icc current calibrations automation in Windows

I have a problem where Windows discards custom ICC display calibrations whenever an external monitor is hot-plugged into my system. I believe this applies to both Windows 7 and Windows Vista. I want to automate pushing the "Reload current calibrations" button on the advanced tab of the Color Management control panel applet. Ideally, I'd ...

Powershell function won't work.

I found this function in my old powershell folder: Function listAllPaths([string]$fromFolder,[string]$filter) { Get-ChildItem -Path $fromFolder -Recurse $filter | Select-Object -Property FullName } And wanted to test it out. I put it in my profile, started Powershell and typed the following: PS C:\> listAllPaths("C:\Downloads\Nemi"...

powershell basic syntax question for select

Background: Here the goal is to do some basic commands in powershell using select-string. For some reason, there are certain things that are not working as expected. Assume the following: $vfilter = 'c:/foo/bar/files/*.htm'; Select-String -path $vfilter -pattern ".*DOCTY.*" | sort LineNumber | where-object { $_...

FTP error in powershell

I'm writing a fairly simply script and I'm trying to upload a file to an ftp server. I'm using the following command ftp -n -s:upload.txt 292.78.51.12 (I've faked the IP) It works perfectly in command prompt but it seems to have a problem with it in powershell. I've tried giving the absolute path to upload.txt, put .\ before it and no...