Powershell 4 Get-ScheduledTask and Windows - scheduled-tasks

I thought no matter what OS you had, if you had Powershell installed, you would have access to the same default cmdlets.
So I want to use Get-ScheduledTask on my Windows 7 machine. I have Powershell 4 installed. However, when I run it, I get the error:
Get-ScheduledTask : The term 'Get-ScheduledTask' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-ScheduledTask
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-ScheduledTask:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
If I run on my Windows 8.1 with Powershell 4 already pre-installed along with the OS, it works.
Can I get the cmdlets on my Windows 7 machine? There is nothing on the Microsoft Get-ScheduledTask page about Windows 7 so I am guessing not.
If not then would it be a case of using the command line:
scheduled task command line
No doubt someone will point me at this question but that was for Powershell 2. I am on Powershell 4.
Now I am a big fan of not reinventing the wheel, but this guys scripts look a good alternative.

Get-ScheduledTask relies on underlying features of the OS that Windows 7 doesn't have, so there is no way to run the cmdlet on that OS, even with PowerShell v4. In your case, you can either leverage schtasks.exe or the Schedule.Service COM object.
This answer that you linked gives the best overview of these methods, but in the interests of completeness, I'll link the relevant resources here:
schtasks.exe
MS PowerShellPack TaskScheduler module -> (leverages the Schedule.Service COM object)

Related

Powershell built in commands stopped working

I need some help trying to troubleshoot my Powershell.
All started when Formatter won't work on newly created file in VS Code.
I restarted all instances of VS Code - and it stopped working everywhere.
I tried to disable and re-install powershell extension - nothing.
VS Code froze on "Starting Powershell.." in bottom right corner and when clicked it says:
Running the contributed command: 'PowerShell.ShowSessionMenu' failed.
When I start Powershell console or Powershell ISE even as Admin, half of the commands not working:
Write-Host : The term 'Write-Host' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Write-Host "test"
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Write-Host:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Laptop updated to KB4592449 4 days ago and instances of Code and Powershell ISE that I was using normally until today were running since then,
Any idea what happened and how to fix?
Update: Everything returned to norm after I uninstalled Powershell 7 AND PowerToys. Not sure which one helped.
Update2: Nope, after couple of hours same symptoms.
Update3: Troubleshooting further. Ran (Get-Command -type cmdlet Write-Host).ModuleName on a healthy computer, got Microsoft.PowerShell.Utility
Ran Get-Module "Microsoft.PowerShell.Utility" on bad computer - no output, meaning module is not loaded. because on healthy computer it did show some output for that module. Why is not this module imported?
If I say Import-Module Microsoft.PowerShell.Utility on bad computer, Write-Host starts working again.
Why is it not autoloading as always?

Why do I get an error running Get-WindowsCapability in PowerShell ISE on Windows Server 2012 r2?

I am running the PowerShell ISE on a Windows Server 2012 r2 machine.
When I run the following:
Get-WindowsCapability
I get the following error:
Get-WindowsCapability : The term 'Get-WindowsCapability' is not
recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again. At line:1 char:1
Get-WindowsCapability
+ CategoryInfo : ObjectNotFound: (Get-WindowsCapability:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Am I missing a PowerShell package?
How do get this feature working?
Thanks, JohnB
Get-WindowsCapability is a cmdlet from the DISM module.
DISM was deployed natively with Server 2012 R2, but depending on the version of PowerShell or other configuration settings (like where your Windows partition is setup or image specific customizations), sometimes we find that we need to import the module before using it, like so:
Import-Module DISM
#or, to see which commands are in the module
Import-Module DISM -Verbose
If this fails, double check that it wasn't removed somehow. For instance, does it appear when you run the following?
Get-Module DISM -ListAvailable
If it doesn't appear, then it seems the module was removed and maybe the Windows component. No worries, we can get it back with the module too by installing the Windows Automated Deployment Kit (ADK), found here.
Update: you may find the module under this path as well
C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\.
The module file to import would be under the x86\DISM folder or x64\DISM, and named dism.psm1
This command seems not to be available in Windows Server 2012. For a list of all available commands, see Deployment Image Servicing and Management (DISM).

Powershell .ps1 scripts won't run / don't exist?

I’ve started having the strangest problem with running powershell scripts in the last few weeks…
They all display this error message as if the file doesn’t exist?
Full console output below showing that I proved the file does exist with the Get-Content cmdlet…
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS H:\> C:
PS C:\> cd C:\Users\Public
PS C:\Users\Public> .\test3.ps1
.\test3.ps1 : The term '.\test3.ps1' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ .\test3.ps1
+ ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\test3.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\Public> get-content test3.ps1
Get-Process | Out-GridView -Wait
PS C:\Users\Public> .\test3.ps1
.\test3.ps1 : The term '.\test3.ps1' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ .\test3.ps1
+ ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\test3.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\Public>
I’ve already got my execution policy set to unrestricted.
Any suggestions
OS
Windows 10 Enterprise
Version 1909 (OS Build 18363.959)
PS C:\Users\Public>> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.18362.752
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.752
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
UPDATE:
I can get a different result when set to run in PowerShell version 2...
PS C:\Users\Public> powershell -version 2 -ExecutionPolicy bypass -file test3.ps1
Out-GridView : A parameter cannot be found that matches parameter name 'Wait'.
At C:\Users\Public\test3.ps1:1 char:33
+ Get-Process | Out-GridView -Wait <<<<
    + CategoryInfo          : InvalidArgument: (:) [Out-GridView], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.OutGridViewCommand
PS C:\Users\Public> powershell -version 3 -ExecutionPolicy bypass -file test3.ps1
The term 'C:\Users\Public\test3.ps1' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (C:\Users\Public\test3.ps1:String) [], ParentContainsErrorRecordExceptio
   n
    + FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\Public>
Presumably the -Wait argument just wasn't available in version 2. I can use this now to run scripts in version 2, but obviously that's quite limited functionality.
UPDATE 2:
I had completely forgot that I had this exact problem earlier in the year; and even posted a Question about it over on Superuser…
https://superuser.com/questions/1536086/powershell-wont-run-ps1-files
I had marked up an answer that I thought it was caused by the transition from using CAG to CheckPointVPN (back in March when remote-working was becoming a long-term thing); and it was somehow resolved after going into the office and connecting directly to the network?
This time however; connecting directly to the office network isn’t resolving it.
Only thing else like that that has changed recently; was that IT setup support for Windows Password 30 day resets when working remotely (previously it wasn’t working, so the 30 day reset was previously disabled during lockdown).  I had reset my password remotely just before taking leave in August, and when I came back I was unable to run powershell scripts?
I have tried resetting my password while in the office, but it hasn’t fixed the problem.
I’m stuck for what else I can check??
This problem was down to AppLocker preventing the script from running.
In order to check if this is the cause for your problem, open the Event Viewer and navigate to
> Applications and Services Logs
> > Windows
> > > AppLocker
> > > > MSI and Script
Look for any Error log entries and it should state something like
%OSDRIVE%\USERS\PUBLIC\TEST3.PS1 was prevented from running.
In my case this change was applied by IT security 2 months back; and since then the IT helpdesk have been slowly dealing with the complaints and whitelisting selected folder directories to resolve.
For some reason my colleagues weren't impacted by the change when they should have been; which lead me to believe it was a problem with my machine or user.
In order not to let this question go by without answer, here my comment as answer
Seeing what you have already tried, I think there is a problem with one of the dll's in your system. Since PowerShell is part of Windows 10, you cannot uninstall/reinstall it as far as I know.
What you can try is to repair missing or corrupted system files.
Open an elevated command prompt and run
dism /online /cleanup-image /restorehealth
This will use Windows Update to provide the files that are required to fix corruptions, so an internet connection is required. If that is out of the question, you can specify another source with switch
/Source:<PathToFreshWindowsFiles>
When that finishes, type
sfc /scannow
to scan all protected system files, and replace corrupted files with a cached copy that is located in a compressed folder "%WinDir%\System32\dllcache"
Both actions take quite some time to finish.
When done reboot your computer.
In my case, I had this error and the issue was a file the script was trying to access did not exist. I ran the script in powershell_ise.exe and clicked the Run Script button and a much clearer message was displayed:
Cannot find path 'C:\logs\logfile.log' because it does not exist.
I created the file and the script ran successfully.

get-odbcdsn in Windows 7

Is it possible to run get-odbcdsn in Windows 7?
Powershell in windows 7 does not seem to understand the cmdlet.
PS C:\Windows\system32> get-help "odbc"
get-help : Get-Help could not find odbc in a help file in this session. To download updated help topics type:
"Update-Help". To get help online, search for the help topic in the TechNet library at
http://go.microsoft.com/fwlink/?LinkID=107116.
At line:1 char:1
+ get-help "odbc"
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-Help], HelpNotFoundException
+ FullyQualifiedErrorId : HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand
I have tried to update-help but it is still not getting any further.
Running Get-odbcdsn caused this error:
Get-OdbcDsn : The term 'Get-OdbcDsn' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I have updated my powershell in windows 7
PS C:\Windows\system32> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 0 10586 117
How do I run get-odbcdsn in windows 7? Please help.
If get-odbcdsn could not be run in windows 7, is there a way to get around the issue? I need to eventually create odbc connection for 100 workstations, most of them have windows 7 on it. Is there a way to do that through powershell?
As pointed out, the 'Get-ODBCDSN' cmdlet requires Windows 8/Server 2012 or higher.
In lieu of this, you could use the "Configure a Data Source Item" in Active Directory Group Policy to achieve this.
Or failing that, you could add the required registry keys directly under HKLM\SOFTWARE\ODBC\ODBC.INI.
Even if you have updated your version of PowerShell you do not get the addition cmdlets that you would assume would be included.
The reason for that is cmdlet require OS level APIs to be present for them to function, in this case the MSFT_OdbcDsn WMI class that was first introduced in Windows 8 and Server 2012, and are not included in Windows 7 and earlier.

Test to check if Cmdlet is Supported

I have a powershell script that keeps throwing this error
Get-ComputerRestorePoint : This functionality is not supported on this operating system.
At line:1 char:25
+ Get-ComputerRestorePoint <<<<
+ CategoryInfo : InvalidOperation: (:) [Get-ComputerRestorePoint], ArgumentException
+ FullyQualifiedErrorId : GetComputerRestorePointNotSupported,Microsoft.PowerShell.Commands.GetComputerRestorePoin
tCommand
Is there a simple way to test to see if a cmdlet is supported before running it? I know I can throw a try catch around the cmdlet, but it this approach seems rather expensive compare to a simple check.
Thanks
You can easily check if a cmdlet is available in your session (using Get-Command) but there is not a direct way to test if an available cmdlet supports the current OS. The help notes section for this cmdlet says:
NOTES
To run a Get-ComputerRestorePoint command on Windows Vista and later versions
of Windows, open Windows PowerShell with the "Run as administrator" option.
This cmdlet uses the Windows Management Instrumentation (WMI) SystemRestore
class.
However I wouldn't rely on the Notes being accurate. The best I can think of is what you suggest, use try/catch.