blumi Posted February 26, 2016 Posted February 26, 2016 Have read a lot of Powershell topics here, but don't understand everything. Run a cmd with Powershell in it, run a .ps1 file, or run powershell code. If possible, I don't want to use an external file with code in it. Just want to put the code in the autoit script. Here the code setlocal enableextensions powershell -command "Get-CimInstance -Class Win32_UserProfile | Where { $_.LocalPath -eq 'C:\Users\setup' } | Remove-CimInstance" How to handle this? Thanks a lot.
water Posted February 26, 2016 Posted February 26, 2016 I do it this way: $sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ...." $pid = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD) My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
iamtheky Posted February 26, 2016 Posted February 26, 2016 I'd also recommend separating items that have quotation requirements, in this case the local path wrapped with single quotes only needs to be passed rather than mucking about with formatting on the line that executes the command. #include<AutoItConstants.au3> $sPath = "'C:\Users\" & @UserName & "'" $sCommand1 = 'powershell -command Get-CimInstance -Class Win32_UserProfile | Where { $_.LocalPath -eq ' & $sPath & ' }' $iPID = run($sCommand1, "" , @SW_HIDE , $stdout_child) $sOutput = "" While 1 $sOutput &= StdoutRead($iPID) If @error Then ExitLoop WEnd ProcessClose($iPID) msgbox(0, '' , $sOutput) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
blumi Posted February 29, 2016 Author Posted February 29, 2016 On 26.02.2016 at 1:49 PM, water said: I do it this way: $sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ...." $pid = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD) This one works fine, thank you.
blumi Posted February 29, 2016 Author Posted February 29, 2016 On 26.02.2016 at 3:21 PM, iamtheky said: I'd also recommend separating items that have quotation requirements, in this case the local path wrapped with single quotes only needs to be passed rather than mucking about with formatting on the line that executes the command. #include<AutoItConstants.au3> $sPath = "'C:\Users\" & @UserName & "'" $sCommand1 = 'powershell -command Get-CimInstance -Class Win32_UserProfile | Where { $_.LocalPath -eq ' & $sPath & ' }' $iPID = run($sCommand1, "" , @SW_HIDE , $stdout_child) $sOutput = "" While 1 $sOutput &= StdoutRead($iPID) If @error Then ExitLoop WEnd ProcessClose($iPID) msgbox(0, '' , $sOutput) I changed the username but it didn't work.
water Posted February 29, 2016 Posted February 29, 2016 Define: "Does not work". Any errors, any return codes, value of @error? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
blumi Posted February 29, 2016 Author Posted February 29, 2016 I got the msbox, but the folder on the hard disk was not deleted.
iamtheky Posted February 29, 2016 Posted February 29, 2016 (edited) you need to put the "remove-ciminstance" back Edited February 29, 2016 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
blumi Posted March 1, 2016 Author Posted March 1, 2016 When I want to use this Powershell code export-startlayout -as bin -path c: \customstartscreenlayout.bin –verbose export-startlayout -as bin -path c: \customstartscreenlayout.bin –verbose I tried something like this, but it didn't work and I am not sure about the correct syntax. $sCommand1 = 'powershell -command export-startlayout -as bin -path c:\tmp\customstartscreenlayout.bin –verbose' $iPID = run($sCommand1, "" , @SW_SHOW , $stdout_child)
water Posted March 2, 2016 Posted March 2, 2016 Either use "ShellExecute" to run your above code or specify the full path to Powershell.exe as I described in post #2. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
iamtheky Posted March 2, 2016 Posted March 2, 2016 This is a redirection issue, which cascades it to a rights issue, which presents the user with the error the cmdlet does not exist. ;powershell test #requireadmin DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1) $sCommand1 = 'Powershell.exe Export-StartLayout -As bin -Path c:\temp\test.bin -verbose' run($sCommand1 , @SystemDir) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
MattHiggs Posted March 2, 2016 Posted March 2, 2016 This might be more roundabout than what has already been suggested, but this small script I wrote to use powershell to defrag all fixed and removable drives shows my method $drives = DriveGetDrive ( $DT_ALL ) For $i = 1 To $drives[0] Step 1 If DriveGetType ( $drives[$i] & "\" ) == "Fixed" Or DriveGetType ( $drives[$i] & "\" ) == "Removable" Then $letter = StringLeft ( $drives[$i], 1 ) RunWait ( @ComSpec & ' /c @powershell -c "Optimize-Volume ' & $letter & ' -Verbose"', @SystemDir ) EndIf Next
blumi Posted March 3, 2016 Author Posted March 3, 2016 17 hours ago, iamtheky said: This is a redirection issue, which cascades it to a rights issue, which presents the user with the error the cmdlet does not exist. ;powershell test #requireadmin DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1) $sCommand1 = 'Powershell.exe Export-StartLayout -As bin -Path c:\temp\test.bin -verbose' run($sCommand1 , @SystemDir) Tried this, but I see only for a short time the powershell windows with red font in it, the folder is empty, no test.bin in it. Did this work for you ?
blumi Posted March 3, 2016 Author Posted March 3, 2016 18 hours ago, water said: Either use "ShellExecute" to run your above code or specify the full path to Powershell.exe as I described in post #2. Tried something like this, didn't work $sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" $para = " export-startlayout -as bin -path c:\tmp\customstartscreenlayout.bin –verbose" $pid = ShellExecute($sCMD, $para, @SystemDir, "", @SW_MAXIMIZE) Seems to be more complicated than I thought to use this single line of code in AutoIT Will there be better powershell support in AutoIT in future?
blumi Posted March 3, 2016 Author Posted March 3, 2016 15 hours ago, MattHiggs said: This might be more roundabout than what has already been suggested, but this small script I wrote to use powershell to defrag all fixed and removable drives shows my method $drives = DriveGetDrive ( $DT_ALL ) For $i = 1 To $drives[0] Step 1 If DriveGetType ( $drives[$i] & "\" ) == "Fixed" Or DriveGetType ( $drives[$i] & "\" ) == "Removable" Then $letter = StringLeft ( $drives[$i], 1 ) RunWait ( @ComSpec & ' /c @powershell -c "Optimize-Volume ' & $letter & ' -Verbose"', @SystemDir ) EndIf Next Tried this RunWait ( @ComSpec & ' /c @powershell Export-StartLayout -As bin -Path c:\temp\test.bin -verbose', @SystemDir ) and RunWait ( @ComSpec & ' /c @powershell -c Export-StartLayout -As bin -Path c:\temp\test.bin -verbose', @SystemDir ) Nothing works
water Posted March 3, 2016 Posted March 3, 2016 Please re-check my post #2. I didn't suggest to use ShellExecute. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
water Posted March 3, 2016 Posted March 3, 2016 1 minute ago, blumi said: Tried this RunWait ( @ComSpec & ' /c @powershell Export-StartLayout -As bin -Path c:\temp\test.bin -verbose', @SystemDir ) and RunWait ( @ComSpec & ' /c @powershell -c Export-StartLayout -As bin -Path c:\temp\test.bin -verbose', @SystemDir ) Nothing works Seems you try every possible combination Except the one that works What is @powershell? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
blumi Posted March 3, 2016 Author Posted March 3, 2016 2 minutes ago, water said: Seems you try every possible combination Except the one that works What is @powershell? I am no expert, but I am sure you know this after reading my posts. ;-) @powershell, never seen before. It is no macro reference of autoit.
water Posted March 3, 2016 Posted March 3, 2016 Correct, but you are using it in your script (post #18). What do you expect it to do? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now