Windows Server 2012R2 Core - can't run powershell DSC - powershell

After installing a fresh Windows Server 2012R2 Core installation I created a sample DSC to test and received: Term "Setup" is not recognized as a name ...
c:\src\dsc.ps1
Configuration Setup
{
Node .
{
WindowsFeature "IIS"
{
Ensure = "Present"
Name = "Web-Server"
}
}
}
C:\src> .\dsc.ps1
C:\src> Setup
Setup : The term 'Setup' 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
+ Setup
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (Setup:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
On my local Windows machine this works without any issues. Is there something I need to install or configure to make this work?

There is a small mistake in the line:
.\dsc.ps1
it should say
. .\dsc.ps1
to load the Setup configruation into the current session so you can use it.

Related

databricks-cli installed, modules not visible

I installed the Databricks CLI, but I am unable to work with the library as such:
In powershell, I have set the working directory to: C:\Users\DNaught1\AppData\Local\Programs\Python\Python39\Scripts
I know the module is there:
Mode LastWriteTime Length Name
-a---- 13/10/2020 1:46 PM 74752 databricks.exe**
I then try to see the version but no luck as shown below:
PS C:\Users\DNaught1\AppData\Local\Programs\Python\Python39\Scripts> databricks --version
databricks : The term 'databricks' 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
databricks --version
+ CategoryInfo : ObjectNotFound: (databricks:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: The command databricks was not found, but does
exist in the current location. Windows PowerShell does not load
commands from the current location by default. If you trust this
command, instead type: ".\databricks". See "get-help
about_Command_Precedence" for more details.
I managed to work around it by using the following:
1- In Python use the command below to install the CLI module
package_names=['databricks-cli', 'requests'] #packages to install
pip.main(['install'] + package_names + ['--upgrade'])
2- In powershell cli:
change the context to the working directory:
Set-Location -Path C:\Users\DNaught1\AppData\Local\Programs\Python\Python39\Scripts
3 - Configure the databricks cli:
.\databricks.exe configure--token
.\databricks configure --token
Host: https://xxx.azuredatabricks.net
Token: dapi2b2dxxxxxxxxxa02c9e6866d322 - Access token for Secrets
4 - Write-host Configuration file for databricks access
Set-Content .databrickscfg "[DEFAULT]"
>> Add-Content .databrickscfg "host = https://xxx.azuredatabricks.net"
>> Add-Content .databrickscfg "token = dapi2b2dxxxxxxxxxa02c9e6866d322"
5 - Validate by checking file storage
.\dbfs ls

Why do proxy commands handle errors differently

For a while, I am maintaining a PowerShell Join-Object cmdlet.
In here I am creating a few proxy commands with default parameters, as FullJoin-Object, Merge-Object and Insert-Object as described here: Proxy Functions: Spice Up Your PowerShell Core Cmdlets.
(In earlier version I was using aliases which could problems if the user creates its own aliases.)
Everything works as expected except that the error handling differs a little between the main command and the proxy command...
Taken the following MVCE, based on the following function:
Function Inverse([Int]$Number) {
Rubbish
Write-Output (1 / $Number)
}
(Where the function Rubbish doesn't exist)
Than I create a proxy function called Reverse0:
$MetaData = [System.Management.Automation.CommandMetadata](Get-Command Inverse)
$Value = [System.Management.Automation.ProxyCommand]::Create($MetaData)
$Null = New-Item -Path Function:\ -Name "Script:Inverse0" -Value $Value -Force
$PSDefaultParameterValues['Inverse0:Number'] = 0 # (Not really required for reproducing the issue)
If I run the original function Reverse 0, I get two errors:
Rubbish : The term 'Rubbish' 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:2 char:1
+ Rubbish
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (Rubbish:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Attempted to divide by zero.
At line:3 char:1
+ Write-Output (1 / $Number)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
If run the proxy command Reverse0 (or Reverse0 0), I get only the first error:
Rubbish : The term 'Rubbish' 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:2 char:1
+ Rubbish
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (Rubbish:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
It seems that something like the $ErrorActionPreference has changed but I checked that and it appears to be the same within both functions.
Is there and explanation for the different behavior?
Is there a way to get a Proxy Command act the same as the original command with respect to error handling?
The code that [System.Management.Automation.ProxyCommand]::Create() generates is currently (PowerShell Core 7.0.0-preview.5) flawed:
It incorrectly propagates statement-terminating errors as script-terminating errors, by using throw rather than $PSCmdlet.ThrowTerminatingError($_) in its catch blocks, causing the function to abort instantly.
If you manually correct these calls, your proxy function should behave as expected.
See this GitHub issue; the linked issue isn't specifically about this behavior, but a change has been green-lighted, and it should include a fix for it.
In concrete terms, for now, you'll have to fix the generated code manually:
Locate all try / catch statements that look like this:
try {
# ...
} catch {
throw
}
and replace them with:
try {
# ...
} catch {
$PSCmdlet.ThrowTerminatingError($_)
}

why is powershell complaining about commented lines?

I have the following comment in a powershell script:
#ERROR: Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
#EXPLANATION: PowerShell calls uses TLS 1.0 for web requests by default.
# However, Exchange is expecting a higher level of TLS, so you need to tell PowerShell to use 1.2 instead of the default of TLS 1.0
​#SOLUTION(s):
# [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
when i run the script i get this error:
s : The term 's' 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 ps1:4 char:14
+ ​#SOLUTION(s):
+ ~
+ CategoryInfo : ObjectNotFound: (s:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
why is it complaining about a comment?
if i remove (s) it then complains about SOLUTION itself...
You have it posted in your error log: + ​#SOLUTION(s):
There is a null character before the # in your code to the left of SOLUTION(s):, so it is reading the line into powershell. Just delete the row ​#SOLUTION(s): and retype it.
To check copy-and-paste the line into powershell console, should read out like below:

how connect to Azure Machine Learning Web service using PowerShell?

To use Azure Machine Learning Web service here you can find some sample code in C#, R, Python and JavaScript. I want to use it in PowerShell.
I found this tutorial, but when I am running bellow line of code, it will return error that it is not recognized:
Set-AzureMLWebServiceConnection -URI $Url -APIKey $API_key
Output:
Set-AzureMLWebServiceConnection : The term 'Set-AzureMLWebServiceConnection' 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 C:\Users\Reza\Desktop\ndbench\Azure\Automation\01_get_metrics\add_target_to_tables - runbook_01.ps1:33 char:1
+ Set-AzureMLWebServiceConnection -URI $Url -APIKey $API_key
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-AzureMLWebServiceConnection:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I can't found Set-AzureMLWebServiceConnection in my PowerShell command-list and I don't know how I can enable/install it.
Can you please guide me, how I can connect to Azure Machine Learning Web service using PowerShell?
The comment #gvee mentioned may be the best to use going forward though it is in beta.
However, to answer your question, use the Install-Module -Name AzureML command to get access to the Azure ML commands.

Powershell Error when run on a different machine

I wrote a script to take an AD user, disable the user, remove the user from group memberships and move the user to an OU. I originally wrote this on our Windows 2008 R2 DC (I know, bad idea) and I wanted to run the script locally on my Win 7 SP1 machine. It has the AD role installed as stated in this article (http://blogs.msdn.com/b/rkramesh/archive/2012/01/17/how-to-add-active-directory-module-in-powershell-in-windows-7.aspx)
I ran on both the DC and my Win7 machine $PSVersionTable and they are exactly the same. I can run ADSIEDIT.msc on the Win 7 machine. The error is occurring when doing an AD user lookup. See error output below:
Here is my script: https://github.com/nocode99/powershell/blob/master/UserDisableGroupRemoval.ps1
Property 'filter' cannot be found on this object; make sure it exists and is settable.
At C:\Admin\test.ps1:23 char:12
+ $ADsearch. <<<< filter = "(&(objectClass=user)(sAMAccountName=$user))"
+ CategoryInfo : InvalidOperation: (filter:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
You cannot call a method on a null-valued expression.
At C:\Admin\test.ps1:24 char:32
+ $ADfind = $ADsearch.findOne <<<< ()
+ CategoryInfo : InvalidOperation: (findOne:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any ideas? The ActiveDirectory module imports with no issues and I want my users to run this locally on their machine rather than the DC.
Looks like I needed to include a filter before the lookup and added:
$adsearch = [adsisearcher]""
though I'm not sure why this works without the filter on AD server itself.