Hiding Diskpart window & Capturing output.

Hello everyone,

I have some users who need to have a VHD connected to a local mount point when they logon to a VM. It all works OK at the moment with a straightforward batch file, but I want to move it into PS to

a) stop a new console window appearing with the Diskpart output, and

b) capture the Diskpart output to a text file for each user (and add some more error checking etc later in the script)

At the moment it work as expected when a local admin logs on (ie no console, and output to file), but for a normal user it still opens a new console window for the Diskpart. It does create the DiskpartOutput.txt file, but it does not redirect the Diskpart output to the file. (NB Diskpart does run OK for normal users, as we have given all users access to run it)

Write-Host "`n Attaching VHDs..."
Start-Process diskpart.exe -ArgumentList "/S \\networkshare\config\diskpart.txt" -RedirectStandardOutput $UserVhomeshare\DiskpartOutput.txt -wait -NoNewWindow

it also has to wait until the diskpart has finished before continuing with the script, as the VHD's contain SQL data which is needed by the next step which starts the SQL services...

Any ideas gratefully appreciated :-)

David.

February 14th, 2015 1:45pm

To mount a VHD with PowerShell just mount it.  There is no need to use diskpart.

Mount-VHD -path c:\test\testvhdx.vhdx

Free Windows Admin Tool Kit Click here and download it now
February 14th, 2015 3:04pm

Thanks JRV, but unfortunately I don't have PS4.0 on these Win7 VMs as yet, so I am stuck with Diskpart at the moment... 
February 14th, 2015 7:36pm

Thanks again JRV, but as I mentioned it's not a problem for the non-admin users actually running diskpart, we give them that permission through Appsense, it is just that the diskpart output window pops up while the PS script is running and the output does not get sent to the output text file... which doesn't happen when an admin user runs it.

For both types of users, the actual diskpart commands themselves complete.. I just want to get rid of the output window and have it go to the text file for the non-admin users too.

February 14th, 2015 11:06pm

Security on many admin level programs prevent them from running hidden.

Try this:

$arglist=@('/S \\networkshare\config\diskpart.txt'

$results=Start-Process -FilePath diskpart.exe  -NoNewWindow  -ArgumentList $arglist 

$results | Out-File "$UserVhomeshare\DiskpartOutput.txt"

I don't believe you can use both wait and NoNewCWindow.  "NoNewWindow" implies "Wait".

Note also that you cannot run this remotely.

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 12:08am

Hi jrv,

Many thanks..again! That behaves the same way unfortunately, but that is interesting info about the wait parameter, thanks, and I am starting to think something built in is preventing diskpart from running hidden for the non-admins, as you suggest.

Also tried upgrading to PS4, but it appears that the VHD cmdlets don't come on Win7..

Cheers,
David

February 15th, 2015 12:16pm

Ha, this hides the console window.... using the cunningly named "-WindowStyle Hidden"  :-)

http://blogs.technet.com/b/heyscriptingguy/archive/2014/02/16/powertip-start-hidden-process-with-powershell.aspx

Start-Process diskpart.exe -ArgumentList "/S \\networkshare\config\diskpart.txt" -RedirectStandardOutput $UserVhomeshare\DiskpartOutput.txt -wait -WindowStyle Hidden

But it still doesn't send the diskpart output to the text file (for the non-admin users)



  • Edited by dthomas1769 Sunday, February 15, 2015 11:07 AM
Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 2:06pm

Hi Dthomas,

I also would like to know if this script can print the result in pwershell console:

Start-Process diskpart.exe -ArgumentList "/S \\networkshare\config\diskpart.txt" -NoNewWindow

Please also check if the non-admin user has enough permission to read the script from the input file and write the results to the output file in your script in te cmdlet "Start-Process".

If there is anything else regarding this issue, please feel free to post back.

If you have any feedback on our support, please click here.

Best Regards,

Anna Wang

TechNet Community Support

February 22nd, 2015 9:05am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics