blumi Posted February 26, 2016 Share 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. Link to comment Share on other sites More sharing options...
water Posted February 26, 2016 Share 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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
iamtheky Posted February 26, 2016 Share 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) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
blumi Posted February 29, 2016 Author Share 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. Link to comment Share on other sites More sharing options...
blumi Posted February 29, 2016 Author Share 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. Link to comment Share on other sites More sharing options...
water Posted February 29, 2016 Share 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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
blumi Posted February 29, 2016 Author Share Posted February 29, 2016 I got the msbox, but the folder on the hard disk was not deleted. Link to comment Share on other sites More sharing options...
iamtheky Posted February 29, 2016 Share Posted February 29, 2016 (edited) you need to put the "remove-ciminstance" back Edited February 29, 2016 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
blumi Posted March 1, 2016 Author Share 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) Link to comment Share on other sites More sharing options...
blumi Posted March 2, 2016 Author Share Posted March 2, 2016 Any ideas, how to run this code? Link to comment Share on other sites More sharing options...
water Posted March 2, 2016 Share 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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
iamtheky Posted March 2, 2016 Share 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) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
MattHiggs Posted March 2, 2016 Share 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 Link to comment Share on other sites More sharing options...
blumi Posted March 3, 2016 Author Share 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 ? Link to comment Share on other sites More sharing options...
blumi Posted March 3, 2016 Author Share 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? Link to comment Share on other sites More sharing options...
blumi Posted March 3, 2016 Author Share 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 Link to comment Share on other sites More sharing options...
water Posted March 3, 2016 Share 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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
water Posted March 3, 2016 Share 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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
blumi Posted March 3, 2016 Author Share 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. Link to comment Share on other sites More sharing options...
water Posted March 3, 2016 Share 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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
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