Using Set-Date in a Powershell script fails due to permissions - windows

When trying to run Set-Date in my powershell script it fails because it claims to require more permissions. I have added the groups Users and even Everyone to the Adjust system time Local Security Policy. Of course I could run it elevated to admin, but I'm trying to avoid that. What's extra odd is that this script has worked fine so far on some machines.
Powershell version is 5.1, windows version is 10 Enterprise 2016 (v1607)
The error message:
PS > Set-Date "2021-06-07T10:00"
Set-Date : Klienten har ikke nødvendig tilgangsnivå
At line:1 char:1
+ Set-Date "2021-06-07T10:00"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Set-Date], Win32Exception
+ FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.SetDateCommand

Though I do not fully understand why, it works when you disable the policy User Account Control: Run all administrators in Admin Approval Mode.

Related

How to fix error while using sanity init coupon?

I want to build a new project using sanity. I tried this command - npm install -g #sanity/cli and it was successfully installed. But when I tried this command - sanity init --coupon javascriptmastery2022, I get the following error:
sanity : File C:\Users\asus\AppData\Roaming\npm\sanity.ps1 cannot be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ sanity init --coupon javascriptmastery2022
+ ~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
Now how should I fix this error?
Start powershell as administrator and go to the folder where you want to install sanity.
Type: Set-ExecutionPolicy -Scope CurrentUser
Then change to Unrestricted
It should work now
Run this command in bash in the backend Sanity folder

Corrupt PackageManager? Unable to find module providers (PowerShellGet)

This is a common error that is not solved by the common solutions I have found on the internet.
Unable to run Get-PsRepository, Install-Module, and related (OneGet?) commands without them throwing the "Unable to find module providers (PowerShellGet)" error. This appears to be an issue with the NuGet packagemanager module but I do not know how to recover. The package provider installed appears valid
Symptom patterns:
C:> [Net.ServicePointManager]::SecurityProtocol
Tls12
Get-PSRepository
PackageManagement\Get-PackageSource : Unable to find module providers (PowerShellGet).
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:4496 char:31
+ ... ckageSources = PackageManagement\Get-PackageSource #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Power...etPackageSource:GetPackageSource) [Get-PackageSource
], Exception
+ FullyQualifiedErrorId : UnknownProviders,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackageSource
Register-PSRepository -Default -Verbose
VERBOSE: PowerShell meta provider initialization failed.
VERBOSE: No match was found for the specified search criteria and provider name 'PowerShellGet'. Try
'Get-PackageProvider -ListAvailable' to see if the provider exists on the system.
PackageManagement\Register-PackageSource : Unable to find module providers (PowerShellGet).
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:4211 char:17
+ ... $null = PackageManagement\Register-PackageSource #PSBoundParamete ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Power...erPackageSource:RegisterPackageSource) [Register-Pac
kageSource], Exception
+ FullyQualifiedErrorId : UnknownProviders,Microsoft.PowerShell.PackageManagement.Cmdlets.RegisterPackageSource
Install-Module PowerShellGet -Force
PackageManagement\Install-Package : Unable to find module providers (PowerShellGet).
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
+ ... $null = PackageManagement\Install-Package #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
+ FullyQualifiedErrorId : UnknownProviders,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
Facts:
Windows 10 (1903)
Powershell 5.1.18362.1171
Running PowerShell as Administrator
Not behind a proxy
Have access to https://www.powershellgallery.com/api/v2
Have access to nuget.org
`[Net.ServicePointManager]::SecurityProtocol = Tls12
FIPS is disabled (HKLM\System\CurrentControlSet\Control\Lsa\FIPSAlgorithmPolicy\Enabled DWORD=0)
Get-PackageProvider shows only NuGet 2.8.5.208
PowerShellGet version is 1.0.0.1 ; when trying force a sxs install with Install-Module PowerShellGet -Force I get the same: PackageManagement\Install-Package : Unable to find module providers (PowerShellGet).
We encountered the same problem. Found this thread without a solution. But I dug a bit further and on the computer that had this problem we found version 1.4.7 of PackageManagement installed.
After removing this folder everything started working normally. I have not found how this thing got installed. Seems like a legit powershell module from Microsoft but it's interacting with the 'normal' PowershellGet module and seems to break it.
For us it was located in the %USERPROFILE%/documents/WindowsPowershell/Modules.
Also worth nothing that this %USERPROFILE% was a corporate OneDrive.
Edit: Microsoft have fixed this issue: https://github.com/PowerShell/vscode-powershell/issues/3432
Not enough Rep to comment.
I ended up in the same situation. A Windows 10 1909 machine, with
%USERPROFILE%/document being a corporate OneDrive. Moving
PackageManagement out of /WindowsPowerShell/ changed
Get-PSRepository's output from nothing to PSGallery. Thanks! – tmcg
Mar 1 at 21:35 This was exactly the same issue. I suspect this is
because PowerShell 7 dumped it into this location (possibly Visual
Studio Code when it prompt to update?) – aolszowka Mar 1 at 22:09
I encountered the same issue with packagemanagement in %USERPROFILE%/documents on my personal profile. The file dates matched when I installed VScode and the Powershell Extensions on this new machine.
Removing the folder from modules corrected the issue for the time being.
In addition to removing %USERPROFILE%/Documents/WindowsPowershell/Modules folder, I also found that putting the package manger locally helped powershell.exe -NoLogo -NoProfile -Command 'Save-Module PackageManagement -MinimumVersion 1.4.7 -LiteralPath ""C:\Program Files\WindowsPowerShell\Modules\""' thanks to itsho here https://github.com/PowerShell/vscode-powershell/issues/2824#issuecomment-750902181
The cause is due to the %USERPROFILE%/Documents folder being on a OneDrive and module-install keeps installing the PackageManagement module to user documents by default ignoring PSModulePath, even had the OneDrive location removed/changed.
This my module path from running PS inside vscode identifying info removed:
$env:PSModulePath -split ';'
C:\Users\{myUsername}\OneDrive - {myCompanyName}\{company}\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
There are more things that can be done to fix this from happening again discussed here https://github.com/PowerShell/PowerShell/issues/15552#issuecomment-1002708799
Like setting the documents folder to local and off OneDrive like shown here https://support.microsoft.com/en-us/topic/configuration-of-the-my-documents-folder-dfd9a90d-8f80-18d6-e7cc-f1566fc3b10b
I followed these steps for now.
In explorer to the left under quick access right click Documents, and then click Properties.
Change to the location tab in the configuration box and type the new path to the Documents folder, I used the local one
Click No so the existing files do not move and still can be accessed by going into OneDrive. I also added another quick access folder for my OneDrive location, but that is up to you.
Note: This effects other programs as well like the default save location for PowerPoint. You'll need to be mindful that you don't save something local thinking it will be backed up to OneDrive.
My module paths after the change:
$env:PSModulePath -split ';'
C:\Users\{myUsername}\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
Another workaround could be to have OneDrive keep the files local.
Navigate to %USERPROFILE%/Documents
Right click WindowsPowershell and select "Always keep on this device".
I haven't been able to test this one, but it might be the least impactful way to address this.

Cannot install Chocolatey on Windows 10 version 1903

I recently upgraded my Windows 10 to version 1903, but after some issue and contacting Microsoft technical support, I've reset my Windows. Now I'm trying to reinstall everything that was installed before reset and one of them is Chocolatey (The package manager for Windows).
I tried running both installation command:
(for cmd)
#"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
(for PowerShell)
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
but every time I get this error:
Exception calling "Start" with "0" argument(s): "The specified executable is not a valid application for this OS
platform."
At C:\Users\ehsan\Downloads\install.ps1:206 char:3
+ $process.Start() | Out-Null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : Win32Exception
Exception calling "BeginOutputReadLine" with "0" argument(s): "StandardOut has not been redirected or the process
hasn't started yet."
At C:\Users\ehsan\Downloads\install.ps1:207 char:3
+ $process.BeginOutputReadLine()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
Exception calling "WaitForExit" with "0" argument(s): "No process is associated with this object."
At C:\Users\ehsan\Downloads\install.ps1:208 char:3
+ $process.WaitForExit()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
Unable to unzip package using 7zip. Perhaps try setting $env:chocolateyUseWindowsCompression = 'true' and call install
again. Error: 7-Zip signalled an unknown error (code )
At C:\Users\ehsan\Downloads\install.ps1:220 char:15
+ ... default { throw "$errorMessage 7-Zip signalled an unknown error (co ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Unable to unzip...n error (code ):String) [], RuntimeException
+ FullyQualifiedErrorId : Unable to unzip package using 7zip. Perhaps try setting $env:chocolateyUseWindowsCompres
sion = 'true' and call install again. Error: 7-Zip signalled an unknown error (code )
I also tried downloading the install.ps1 scripts and running it manually (after setting execution policy to bypass, for sure), but the result was the same as above. Worth mentioning that I ran both cmd and PowerShell as administrator for installation. By the way, I didn't try offline installation yet, because I cannot figure out what the instruction says.
Googling the error didn't help me find a solution.
I had this same problem (at least the message was exactly the same). I was installing chocolatey in a more controlled environment, that is, some parts of the scripts are not allowed to be performed by some security policy reason.
The error is that on line 192 of the PowerShell, it's attempted to download an EXE file:
Download-File 'https://chocolatey.org/7za.exe' "$7zaExe"
This is not allowed in my environment, so as I have the Zip already installed on my Windows, I just copy the and renamed the needed file (7az.exe) to the folder that the install.ps1 (Powershell script) was looking for.
In my case:
C:\Users\%USER%\AppData\Local\Temp\chocolatey\chocInstall\7za.exe
So, having the ZIP exe file at this path, with this name, everything worked.
Cheers

gradle version manager tool for windows like pik for ruby

Like pik which provides the facility to switch between ruby versions on the fly from command prompt, is there any tool which provides the same kind of behavior for gradle, so that i can switch to gradle version I'm using based on need.
I googled it, but couldn't find proper solution. So posting here.
Below error I'm getting when use sdkman windows version:
PS C:\Users\user> Import-Module posh-gvm
This command is not available in offline mode.
At C:\Users\user\Documents\WindowsPowerShell\Modules\posh-gvm\Utils.ps1:294 char:14
+ throw <<<< 'This command is not available in offline mode.'
+ CategoryInfo : OperationStopped: (This command is...n offline mode.:String) [], RuntimeException
+ FullyQualifiedErrorId : This command is not available in offline mode.
Import-Module : The specified module 'posh-gvm' was not loaded because no valid module file was found in any module dir
ectory.
At line:1 char:14
+ Import-Module <<<< posh-gvm
+ CategoryInfo : ResourceUnavailable: (posh-gvm:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Yes, there's such tool. It's called sdkman. You can easily switch between versions of gradle, groovy and other available frameworks, languages.

Unable to set certificate friendly name through PowerShell (access is denied)

I'm trying to update a (root) certificate's friendly name through PowerShell.
The certificate I'm trying to update is highlighted in the screenshot below (taken from certmgr.msc):
Here's the PowerShell code (largely taken from here):
Set-Location cert:
cd .\CurrentUser\Root
$cert = gci .\02FAF3E291435468607857694DF5E45B68851868
$cert.FriendlyName = "UserTrust CA"
(I've verified the thumbprint with the certificate properties in certmgr.msc to make sure it's the same certificate).
The last line will result in the following error:
Exception setting "FriendlyName": "Access is denied.
"
At line:1 char:7
+ $cert. <<<< FriendlyName = "UserTrust CA"
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
I've tried the following things so far:
Running PowerShell as administrator
Changing the execution policy using: Set-ExecutionPolicy Unrestricted
Other information:
PowerShell version is 2.0 (running on Windows 7 Enterprise x64)
I'm running the x64 version of PS
DEP and UAC are disabled
My account is part of the Administrators group
I'm a complete PowerShell n00b ;-)
PS version output:
$PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
You need to update your version of Powershell to 3.0 or 4.0.
See Get-ChildItem for Certificate
I have Powershell 4.0 and I was able to change the FriendlyName without problems. This was visible in certmgr.msc after a restart.
You can download Powershell 4.0 here.
If you are a Powershell beginner it's probably better to use Get-ChildItem instead of gci for improved readability. Also, cd is an alias of Set-Location so it's better to choose one or the other in your script to avoid confusion.

Resources