Issue with DSC Script - azure

Just trying to create a script to install IIS and Management Tools and getting the error below, any idea what could be the cause of it?
Configuration iis_dsc_file
{
# Import the module that contains the resource we are using.
Import-DSCResource -ModuleName PsDesiredStateConfiguration
Import-module servermanager
# The Node statement specifices which targets this configuration will be applied to.
Node localhost
{
# Code to ensure IIS feature is enabled
WindowsFeature WebServer
{
Ensure= "Present"
Name= "Web-Server"
}
WindowsFeatures IISManagementTools {
Name= "Web-Mgmt-Tools"
Ensure= "Present"
IncldueAllSubFeature= $True
LogPath= "C:\ServerLogs\IIS-Installation-Log.txt"
}
}
}
The error message I get is below:
The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The term 'WindowsFeatures' 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.
Any idea what is the issue?

It fails because there is an extra S at the end of the second WindowsFeature
By the way, IncldueAllSubFeature is misspelled too, it should be IncludeAllSubFeature

Related

Prevent exec command running on module unload ibm load sharing facilty

I have a tcl script which is a modulefile within the IBM Load Sharing Facitily (lsf) used to configure some environment variables and launch a python script by using the exec command.
When the module is unloaded normally the opposite of all the commands are run, but also the exec command is run as normal. I would like it so that the exec part is only run on module load and not on module unload.
Here is what I tried so far
if { !(is-loaded mymodule)} {
exec .venv/bin/python mypython.py
}
I also tried this
if { module-info command load } {
exec .venv/bin/python mypython.py
}
For each one I get a similar error
Module ERROR: invalid bareword "module"
in expression " module-info command [load] ";
should be "$module" or "{module}" or "module(...)" or ...
both exceptions complain either about an invalid bareword (either "is" or "module") depending on which snippet I try. Is my snytax invalid?
My syntax was incorrect, in the end I was able to solve the problem with the following:
set is_load_command [module-info command load]
if { $is_load_command == 1 } {
exec .venv/bin/python mypython.py
}
I had two problems, correctly understanding comparisons in tcl and using return values from a called function. Neither really behaved how I am used to.

Use custom, composite resource with Azure DSC extension

I have a VM template that deploys a DSC extension. It's been working fine but my configuration is growing so I've refactored it to use a composite resource and republished it to blob storage with Publish-AzureRmVMDscConfiguration.
I verified that the .ps1.zip file in blob storage contains my custom module and that the module is listed under dscmetadata.json. However, when I deploy, the DSC extension fails. The logs under C:\WindowsAzure\Logs\Plugins\Microsoft.Powershell.DSC\2.17.0.0 reveal the reason:
C:\Packages\Plugins\Microsoft.Powershell.DSC\2.17.0.0\bin..\DSCWork\AppServerDev.ps1.0\AppserverDev.ps1
PSDesiredStateConfiguration\node : The module 'xCustomResource' could
not be loaded. For more information, run 'Import-Module
xCustomResource'.
At C:\Packages\Plugins\Microsoft.Powershell.DSC\2.17.0.0\DSCWork\AppServerDev.ps1.0\AppserverDev.ps1:9 char:3
node "localhost"
~~~~
CategoryInfo : ObjectNotFound: (xCustomResource\xCustomResource:String)
[PSDesiredStateConfiguration\node], ParentContainsErrorRecordException
FullyQualifiedErrorId : CouldNotAutoLoadModule,PSDesiredStateConfiguration\node
Here are the relevant bits of my configuration file:
configuration AppServerDev
{
param($environment)
Import-DscResource -ModuleName 'PSDesiredStateConfiguration'
Import-DscResource -ModuleName 'SaaSModule'
node "localhost"
{
LocalConfigurationManager
{
RebootNodeIfNeeded = $true
ConfigurationMode = "ApplyAndAutoCorrect"
ConfigurationModeFrequencyMins = 1440
}
xDCTPlatformVM VM {
OctopusParametersFile = $environment
ChocolateyPackages = #(
'googlechrome',
'notepadplusplus',
'7zip',
'microsoftwse',
'octopusdeploy.tentacle',
'sqlserver-cmdlineutils'
)
}
}
}
Running Get-Module -ListAvailable reveals that DankModule is found and I can, from powershell, run Import-Module DankModule and it works as expected. I assume the confusion is coming from it trying to import xCustomResource rather than DankModule but my .ps1 file under C:\Packages\Plugins\Microsoft.Powershell.DSC\2.17.0.0\DSCWork says
Import-Module "DankModule"
and not
Import-Module "xCustomType"
Why is it trying to import xCustomType rather than DankModule? How do I make it find DankModule which is available and contains xCustomType?
UPDATE: Get-Module -ListAvailable shows DankModule is installed but Get-DSCResource -Module DankModule doesn't return anything.
I'm not sure but I think this is a rookie mistake on my part because I have no experience building powershell modules. I was using this page as well as this one to construct my module and I'd gotten a valid module file by using New-ModuleManifest, I had the correct file structure mentioned in both of them e.g.
C:\Program Files\WindowsPowerShell\Modules\
DankModule
DankModule.psd1
DankModule.psm1
DSCResources
xCustomResource
xCustomResource.psd1
RootModule = ‘xCustomResource.schema.psm1'
xCustomResource.schema.psm1
Configuration, no Node block
but I didn't realize I had some tweaking left. I needed to correct the RootModule property in DankModule.psd1 to look like this:
RootModule = 'C:\Program Files\WindowsPowerShell\Modules\DankModule\DankModule.psm1'
And also needed to add to my blank DankModule.psm1 a line referencing my xCustomResource.schema.psm1 equivalent:
. .\DSCResources\xCustomResource\xCustomResource.schema.psm1
That is the only line in my module-level .psm1 file and everything is now working. I also, at one point, removed all the \0s from both my .psd1 files but now I'm not sure if that was strictly necessary.

I am getting the below error while starting hybrisserver.bat

'findstr' is not recognized as an internal or external command, operable program or batch file. Usage: "G:\HybrisPendrive\Hybris\hybris\bin\platform\tomcat\bin\wrapper.bat" { console : start : pause : resume : stop : restart : install : remove : update } You can give _YWRAPPER_CONF as env. variable to set the path of the used wrapper configuration file. Defaults to '..\conf\wrapper.conf'.
You are probably missing system32 in your PATH variable. Try adding C:\Windows\System32 to your path variable. Info how to do this: here
Good luck

Run powershell commands in C#

RunspaceConfiguration psConfig = RunspaceConfiguration.Create();
Runspace psRunspace = RunspaceFactory.CreateRunspace(psConfig);
psRunspace.Open();
using (Pipeline psPipeline = psRunspace.CreatePipeline())
{
// Define the command to be executed in this pipeline
Command command = new Command("Add-spsolution");
// Add a parameter to this command
command.Parameters.Add("literalpath", #"c:\project3.wsp");
// Add this command to the pipeline
psPipeline.Commands.Add(command);
// Invoke the cmdlet
try
{
Collection<PSObject> results = psPipeline.Invoke();
Label1.Text = "hi"+results.ToString();
// Process the results
}
catch (Exception exception)
{
Label1.Text = exception.ToString();// Process the exception here
}
}
It is throwing the exception:
System.Management.Automation.CommandNotFoundException: The term 'add-spsolution' is not recognized as the name of a cmdlet, function, script file, or operable program.
Any suggestions why?
Add this command first:
Add-PSSnapin Microsoft.SharePoint.Powershell -EA 0
You must use the import-module command to load the correct module for sharepoint. Use get-module to find available modules.
To do this programmatically, see my post on the subject:
http://www.nivot.org/2010/05/03/PowerShell20DeveloperEssentials1InitializingARunspaceWithAModule.aspx
-Oisin
I have this issue recently. In my case I was neither able to see the added solution nor able to add solution. So first I remove solution using below PowerShell Command:
(Get-SPSolution -Identity "YourSolution.wsp").Delete()
Then I was able to add my new code solution.
Also make sure you are runing "Add-SPSolution" command from Web Applications, which is running on IIS, and NOT with standard Visual Studio server (when you press F5).

How do you get the path of the running script in groovy?

I'm writing a groovy script that I want to be controlled via a properties file stored in the same folder. However, I want to be able to call this script from anywhere. When I run the script it always looks for the properties file based on where it is run from, not where the script is.
How can I access the path of the script file from within the script?
You are correct that new File(".").getCanonicalPath() does not work. That returns the working directory.
To get the script directory
scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent
To get the script file path
scriptFile = getClass().protectionDomain.codeSource.location.path
As of Groovy 2.3.0 the #SourceURI annotation can be used to populate a variable with the URI of the script's location. This URI can then be used to get the path to the script:
import groovy.transform.SourceURI
import java.nio.file.Path
import java.nio.file.Paths
#SourceURI
URI sourceUri
Path scriptLocation = Paths.get(sourceUri)
Note that this will only work if the URI is a file: URI (or another URI scheme type with an installed FileSystemProvider), otherwise a FileSystemNotFoundException will be thrown by the Paths.get(URI) call. In particular, certain Groovy runtimes such as groovyshell and nextflow return a data: URI, which will not typically match an installed FileSystemProvider.
This makes sense if you are running the Groovy code as a script, otherwise the whole idea gets a little confusing, IMO. The workaround is here: https://issues.apache.org/jira/browse/GROOVY-1642
Basically this involves changing startGroovy.sh to pass in the location of the Groovy script as an environment variable.
As long as this information is not provided directly by Groovy, it's possible to modify the groovy.(sh|bat) starter script to make this property available as system property:
For unix boxes just change $GROOVY_HOME/bin/groovy (the sh script) to do
export JAVA_OPTS="$JAVA_OPTS -Dscript.name=$0"
before calling startGroovy
For Windows:
In startGroovy.bat add the following 2 lines right after the line with
the :init label (just before the parameter slurping starts):
#rem get name of script to launch with full path
set GROOVY_SCRIPT_NAME=%~f1
A bit further down in the batch file after the line that says "set
JAVA_OPTS=%JAVA_OPTS% -Dgroovy.starter.conf="%STARTER_CONF%" add the
line
set JAVA_OPTS=%JAVA_OPTS% -Dscript.name="%GROOVY_SCRIPT_NAME%"
For gradle user
I have same issue when I'm starting to work with gradle. I want to compile my thrift by remote thrift compiler (custom by my company).
Below is how I solved my issue:
task compileThrift {
doLast {
def projectLocation = projectDir.getAbsolutePath(); // HERE is what you've been looking for.
ssh.run {
session(remotes.compilerServer) {
// Delete existing thrift file.
cleanGeneratedFiles()
new File("$projectLocation/thrift/").eachFile() { f ->
def fileName=f.getName()
if(f.absolutePath.endsWith(".thrift")){
put from: f, into: "$compilerLocation/$fileName"
}
}
execute "mkdir -p $compilerLocation/gen-java"
def compileResult = execute "bash $compilerLocation/genjar $serviceName", logging: 'stdout', pty: true
assert compileResult.contains('SUCCESSFUL')
get from: "$compilerLocation/$serviceName" + '.jar', into: "$projectLocation/libs/"
}
}
}
}
One more solution. It works perfect even you run the script using GrovyConsole
File getScriptFile(){
new File(this.class.classLoader.getResourceLoader().loadGroovySource(this.class.name).toURI())
}
println getScriptFile()
workaround: for us it was running in an ANT environment and storing some location parent (knowing the subpath) in the Java environment properties (System.setProperty( "dirAncestor", "/foo" )) we could access the dir ancestor via Groovy's properties.get('dirAncestor').
maybe this will help for some scenarios mentioned here.

Resources