starting Powershell

Welcome Forums General PowerShell Q&A starting Powershell

Viewing 9 reply threads
  • Author
    Posts
    • #252482
      Participant
      Topics: 43
      Replies: 97
      Points: 593
      Rank: Major Contributor

      Hi all,
      weird question perhaps

      I know you can lauch powershell by using a *.cmd file and this works fine only thing is that you get less output on your screen for example
      write-host “starting outlook”

      start-Process “outlook.exe”

      will just start outlook without showing the write-host comments.
      Is there a way that I can launch powershell so that the users are not able to modify any code but still have a form of output presented to them so that they can see what the current script is doing?
      and can this be done through cmd for example?

       

    • #252605
      Senior Moderator
      Topics: 10
      Replies: 184
      Points: 979
      Helping Hand
      Rank: Major Contributor

      You can use Set-PSDebug to make PowerShell show its work. Set-PSDebug -Trace 1 will cause each line to be echoed as it executes. However, I’m not sure this will be any more effective than Write-Host in cmd.

      To be honest, this sounds like an XY problem. Why are you doing this with cmd rather than the PowerShell console?

    • #252848
      Participant
      Topics: 43
      Replies: 97
      Points: 593
      Rank: Major Contributor

      Hi Grokkit,
      thanks for your reaction,

      let me explain
      I’m working for a second line helpdesk and one of the tasks is taking care of deactivating the users that are leaving the company. as such I’ve automated this through powershell so far so good. Now  people have opted to create a first line helpdesk as well and you guessed it right they have to deal with the leavers.
      I’ve created everything in such a way that you need to open 2 different ps1 files to trigger everything. but opening them in powershell ISE seems a bit tricky for my management so I opted for the cmd solution.
      or I need to make sure that the normal powershell console is started by default when executing the cmd.
      here you have the cmd that I execute to launch powershell at this point it launches the ISE and not the standard powershell console

      NOTE: I’ve removed the formatting to make sure it’s correctly presented, with using the formatting the >> and ” and > are not presented in the correct way.
      PowerShell -Command “Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted” >> “c:\temp\StartupLog.txt” 2>&1
      PowerShell C:\Users\Public\Documents\PowerShellScripts\removedistgroups.ps1 >> “c:\temp\StartupLog.txt” 2>&1

      • This reply was modified 1 month, 4 weeks ago by acer460527.
      • This reply was modified 1 month, 3 weeks ago by grokkit. Reason: code formatting
    • #252908
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      I’ve created everything in such a way that you need to open 2 different ps1 files to trigger everything. but opening them in powershell ISE seems a bit tricky for my management so I opted for the cmd solution.

      I still don’t get why you have to open two separate consoles or Powershell_ISEs? You can easily call one Powershell script from inside the other. And what does this have to do with your management? I thought it’s about a helpdesk. Usually these people should be smart enough to be a ble to start a Powershell session in the appropriate manner. 😉

      NOTE: I’ve removed the formatting to make sure it’s correctly presented, ….

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “CODE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

    • #252992
      Senior Moderator
      Topics: 10
      Replies: 184
      Points: 979
      Helping Hand
      Rank: Major Contributor

      You should not be deploying a script for production use in PowerShell ISE (ever). When you run PowerShell you get a console that has access to the operating system. When you run PowerShell ISE it is a Win32 application that has a console emulator built into it. The console in ISE is not a real execution environment and there are several functions that simply don’t work in it. It’s acceptable for initial testing, but you shouldn’t trust it as a complete test and you definitely shouldn’t use it for normal execution. Basically, the console in ISE is fake. The PowerShell team wrote up a list of differences, which is dated now but gives you an idea of how they don’t match up.

      Ok, now to your actual issue. You’re asking about getting output from a PowerShell script when executing it via a cmd file, but what you actually need is just a simple way to execute a PowerShell script (e.g. by double-clicking an icon). This is an example of an XY problem.

      There is a better solution – you can create a shortcut that will launch your PowerShell script in the Windows PowerShell console. Here’s a discussion from stackoverflow. To make this work, do the following:

      1. On your desktop: right-click > New > Shortcut
      2. Under “Type the location of the item:” set the location to your script, then click “Next”
      3. Give the shortcut a useful name, then click “Finish”
        (you should now have a shortcut that looks like text file with a PowerShell ISE icon – if you double click this it will just open in Notepad for editing – we’ll fix that next)
      4. Right-click your new shortcut icon and select Properties
      5. “Target:” should contain the path to your script – something like this C:\path\script.ps1. Edit it so that it looks like this:
        powershell -Command “& ‘C:\path\script.ps1’ ” -NoProfile
        then click OK
        (your shortcut icon should now look like the PowerShell console icon)
      6. Double-click the shortcut. It should open a new PowerShell window and run your script.

      DISCLAIMER & WARNING
      Double-click execution of PowerShell scripts is normally prevented in Windows for good reason. It creates security issues for scripts to be executable without oversight. If possible, you should change the Write permissions on the shortcut and the script so that they can only be edited with administrative privileges, to prevent the shortcut from being edited to launch other scripts, and to prevent the script from being edited to do things that you don’t want. Setting this up for your one script is effectively the same as trying to do it with a cmd file as you were, and it’s probably an acceptable risk for this use case.

      I don’t recommend that you use the registry change solution that is discussed in that stackoverflow thread, because it will make PowerShell scripts double-click executable across the entire operating system. Setting this up for only one script at a time is much more secure.

      Finally, don’t set ExecutionPolicy to unrestricted, ever. Set it to RemoteSigned instead. This will prevent unsigned scripts from being remotely executed on your system, but will still allow execution of local scripts.

    • #253019
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      I’m just curious – why do you use Powershell -Command … and not Powershell -File … to execute a script file?

    • #253025
      Senior Moderator
      Topics: 10
      Replies: 184
      Points: 979
      Helping Hand
      Rank: Major Contributor

      I’m just curious – why do you use Powershell -Command … and not Powershell -File … to execute a script file?

      Honestly, because that’s how it was done in the stackoverflow answer. However, after reading your comment I tried this with my test shortcut and it fails on my system. The PowerShell window opens and then immediately closes (my test script has some user interaction features, so it shouldn’t just execute and close). I’m not sure why exactly, but I suspect it’s a difference between how the -File and -Command parameters open their targets. The documentation isn’t particularly clear.

    • #253028
      Participant
      Topics: 8
      Replies: 615
      Points: 2,383
      Helping Hand
      Rank: Community Hero

      Did you test with -File?

    • #253673
      Participant
      Topics: 43
      Replies: 97
      Points: 593
      Rank: Major Contributor

      thanks all for your reactions and info

    • #254030
      Participant
      Topics: 25
      Replies: 197
      Points: 718
      Helping Hand
      Rank: Major Contributor

      I’d reccomend picking up and reading the learn powershell toolmaking in a month of lunches.

      Then if you can get a license for powershell studio you’ll be able to easily build a gui with buttons for these interactions, and to provide clearer feed-back on the processes to your end user.

      Alternatively, you could start playing with gui’s in powershell via poshgui.com

Viewing 9 reply threads
  • You must be logged in to reply to this topic.