Error Running Powershell Script Using PSCX - powershell

We're running a Team City build server which already executes this script locally with no problem. However, when I try to run this script manually I have the following problem:
Windows PowerShell
Copyright (C) 2012 Microsoft Corporation. All rights reserved.
PowerShell Community Extensions Imported.
PS C:\Users\user.domain> cd D:\pkg\platform-2b0c7e3f71f9-BUILD01; powershell.exe -noprofile -executionpolicy Bypass -file D:\pkg\platform-2b0c7e3f71f9-BUILD01\stage.ps1 production \\192.168.x.x\staging \\testserver.domain.com\staging
D:\pkg\platform-2b0c7e3f71f9-BUILD01
Start Staging: 12/15/2014 11:18:14
Verifying target environment configuration and staging targets
Copying source and environment switch
...
Verifying configuration files are valid XML files
Test-Xml : The 'Test-Xml' command was found in the module 'Pscx', but the module could not be loaded. For more information, run 'Import-Module Pscx'.
At D:\pkg\platform-2b0c7e3f71f9-BUILD01\stage.ps1:72 char:9
+ if (!(Test-Xml $file))
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Test-Xml:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
So I see the PowerShell Community Extenstions are getting imported (line 4) which makes me wonder what is going on. I am running the script as the exact same user that runs it under Team City. I've checked the build step I'm trying to replicate:
and I can't think of anything that I've missed (working directory option is set to the script file's directory). Any ideas would be appreciated...

Line 4 loads Pscx, Line 5 opens up a new powershell session with -NoProfile option, so it's unlikely that Pscx is loaded in that Powershell session.

Related

How to resolve 'failoverclusters' was not loaded PowerShell script error

I am trying to run the Batch file which runs the SSIS package. The SSIS package call the PowerShell script to fetch the data from different servers. I am able to get the SSIS package working without any error. But whenever I try to run the batch file which calls the SSIS package, it fails with below error:
import-module : The specified module 'failoverclusters' was not loaded because no valid module file was found in any
module directory.
At E:\XYZ\SSISPckg\Hypv\BASE\XYZ70GTNPENG003.ps1:6 char:1
+ import-module failoverclusters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (failoverclusters:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
The term 'Get-ClusterGroup' 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.
no vms in XYZ70GTNPENG003
Microsoft (R) SQL Server Execute Package Utility
Version 15.0.4223.1 for 32-bit
Copyright (C) 2019 Microsoft. All rights reserved.
I did not get such error when I executed the PowerShell script from SSIS.
I was able to identify FailoverClusters module in powershell folder. I was able to locate it by running Get-Module -ListAvailable command.
I have already executed Install-WindowsFeature "RSAT-Clustering-Powershell" to install the required module. Except this module, I was able to import the other Hyper-V module.
So, can someone please help with understanding where I am making mistake. I am able to run the SSIS package which calls the powershell script using Execute Process Task. But if I call the batch file which run the SSIS package, it fails with above error.
The issue is that the FailoverClusters module is only available from the x64 bit version of PowerShell. e.g. when you are opening PowerShell by yourself, it by default, opens the x64 bit version of PowerShell, and hence, is why your testing works. A 32 bit x86 version of the FailoverClusters module does not exist, and can't be run from a 32 bit environment.
You can see this is the case when you launch Windows PowerShell (x86) and try to load the module:
PS C:\> Import-Module failoverclusters
Import-Module : The specified module 'failoverclusters' was not loaded because no valid module file was found in any
module directory.
At line:1 char:1
+ Import-Module failoverclusters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (failoverclusters:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
When running SSIS packages from a .bat file, it defaults to the x86 version, which will launch the 32-bit x86 version of PowerShell, and hence, will throw the error message.
What you have to do is in your .bat file, explicitly launch the SSIS package with the x64 version of DTExec.exe something like this:
& "C:\Program Files\Microsoft SQL Server\150\DTS\Binn\DTExec.exe" /FILE MyFile.dtsx
This will launch the x64 bit version of DTExec.exe which will in turn launch the x64 version of PowerShell, and make the FailoverClusters module available.

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.

Roslyn Setup - Powershell Scripts

I've been trying to install the Rosyln source code from Github and following the build setup instructions.
The first instruction is to run Restore.cmd - which produces the following error message:
File C:\Code\roslyn-master\build\scripts\build.ps1 cannot be loaded.
The file C:\Code\roslyn-master\build\scripts\build.ps1 is not digitally signed.
You cannot run this script on the current system. For more information about
running scripts and setting execution policy, see about_Execution_Policies
at
http://go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo : SecurityError: (:) [],
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess
I have used Set-ExecutionPolicy to disable the checking:
PS C:\Code\roslyn-master> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine Undefined
However, I am still receiving the same error. I'm running Visual Studio 2017 Community on a Windows 10 Pro machine.
Thanks in advance
Jim
The Restore.Cmd file contained the following command:
powershell -noprofile -executionPolicy RemoteSigned -file "%~dp0\build\scripts\build.ps1" -restore %*
It didn't matter what I set the Powershell policy to - the file was going to override it. When I removed the execution policy clause, the file ran fine.

Powershell AzureML Get-AmlWorkspace

Get-AmlWorkspace : One or more errors occurred.
At line:1 char:1
+ Get-AmlWorkspace
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-AmlWorkspace],
AggregateException
+ FullyQualifiedErrorId :
System.AggregateException,AzureML.PowerShell.GetWorkspace
I am trying to use Powershell to connect to Azure ML studio as it looks like an easier way to manage a workspace. I've downloaded the dll file from https://github.com/hning86/azuremlps and changed my config.json file, but get the error above if I try to run any AzureML commands. I've unblocked the DLL file and imported the AzureMLPS module, and I can see the module and commands I am trying to use have been imported by doing Get-Module and Get-Command
For info I've not used Powershell before.
Any suggestions much appreciated!
Have you installed Azure PowerShell Installer on your local machine?
Click here for more info.
Download the latest Azure PowerShell Installer (4.3.1), then install on your local machine. Then retry using Azure PowerShell module and commands.
I installed mine last May, using Azure PowerShell 4.0.1, and the command Get-AmlWorkspace is working.
# Set local folder location
Set-Location -Path "C:\Insert here the location of AzureMLPS.dll"
# Unblock and import Azure Powershell Module (leverages config.json file)
Unblock-File .\AzureMLPS.dll
Import-Module .\AzureMLPS.dll
# Get Azure ML Workspace info
Get-AmlWorkspace
The output on my side looks like this:

Powershell: ExecutionPolicy is unable to be read/set

I was trying to run a powershell script (which I have run before) and received the following error:Authorizationmanager Check Failed
I figured it was the Execution Policy, so ran:
Set-ExecutionPolicy Unrestricted
I then received:
Set-ExecutionPolicy : Initialization failure
At line:1 char:20
+ set-executionpolicy <<<< unrestricted
+ CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], ManagementException
+ FullyQualifiedErrorId : System.Management.ManagementException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
The same initialization error occurs for Get-ExecutionPolicy
Some basic commands seem to work fine, such as:
Write-Output "hi"
Can anyone suggest a fix?
Note: This is on Windows XP
Update: If I run the contents of ascript from within the powershell command prompt, it works. I only get the errors if it is run as a script. Also, Get-ExecutionPolicy and Set-ExecutionPolicy still fail from within the powershell prompt.
Make sure you are running the console as admin [Right click] then [click] "run as administrator" when you run Set-ExecutionPolicy Unrestricted.
The source of the error was due to a problem with the WMI repository. I was able to repair the repository running the following command:
Note: This is for Windows XP
rundll32 wbemupgd, UpgradeRepository
Once this was run, I was successfully able to execute Get-ExecutionPolicy, and other powershell scripts again.
A couple of links that detail repairing the WMI Repository.
Another very common case (I think this only applies post-XP), is where the .ps1 file has been 'blocked' after being downloaded from an untrusted location.
Solution: open the 'Properties' of the file in Windows Explorer, and on the 'General' tab click 'Unblock', then 'Apply' or 'OK'.
Another potential reason for this error (seen on Windows Server 2012) is that the Windows Management Instrumentation service is not running.
Starting and running the service allows for Get-ExecutionPolicy to run, and then Set-ExecutionPolicy.