Home > Net >  Powershell - Run scriptline as current user, and not ICE admin user
Powershell - Run scriptline as current user, and not ICE admin user

Time:02-26

I have a script running different queries through my "admin" AD account from Powershell ICE. And in the end i am exporting my variable to a csv file. Opening the new exported csv file with Excel and applying a custom Excel macro from my PERSONAL.XLSB folder with specified path.

The issue i am having is that whenever i try to make my script open info.csv (array output file).

$FilePath = 'C:\info.csv'
$workbook = $excel.Workbooks.Open($FilePath)

It opens info.csv as my Powershell ICE "admin" AD account logically ofcourse. How can i make $FilePath run-as my current user and not the ps1 admin-AD account?

CodePudding user response:

Run that script in a separate instance of Powershell, that is launched without extra options as "run as administrator" or run as exact user you want that script to run, of course providing credentials if it's not a logged-in user. For example, run a cmd.exe, there launch a powershell and there launch your script via copy paste or sourcing a ps1 file.

CodePudding user response:

This seems to do the trick (tested with simple macro on csv)... Open your Personal.XLSB (non-admin user path) with the same $Excel object as another workbook. Macro will then be available to run against your CSV.

How to use powershell to run personal macro with different paths

$excel = New-Object -comobject Excel.Application
$wbPersonalXLSB = $excel.workbooks.open("C:\Users\[Non-Admin User]\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB")
$FilePath = "[path to csv]\test.csv"
$workbook = $excel.Workbooks.Open($FilePath)
$excel.Visible = $false
$worksheet = $workbook.worksheets.item(1)
$excel.Run("PERSONAL.XLSB![macro name]")
$wbPersonalXLSB.Close()
$workbook.save()
$workbook.close()
$excel.quit()
  • Related