Question

In: Computer Science

Write Powershell Script Verifies System Environment Variable Lmlicensefile Exists Exists S Q42980878 write a PowerShell script...

Write Powershell Script Verifies System Environment Variable Lmlicensefile Exists Exists S Q42980878

write a PowerShell script that verifies the 'System' environmentvariable, 'LM_LICENSE_FILE', exists. If it exists, the scriptshould also verify that the variable [email protected] (which represents the port and server theLM points to) as one of the values in the variable. Values (if anyare present) are separated by commas "," in this variable. Finally,if the value does not exist, this script should also performremediation (add the desired value to the variable, withoutoverwriting the existing values).

Solutions

Expert Solution

Most people know how easy it is to use Windows PowerShell to retrieve information about environment variables. Want to see all your environment variables and their values? This command should do the trick:

Get-ChildItem Env:

In turn, you should get back information similar to this extract:

Name Value
-

--- -----
Path D:\Perl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\...
TEMP C:\DOCUME~1\kmyer\LOCALS~1\Temp
SESSIONNAME Console
PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PS1;.PSC1
USERDOMAIN WINGROUP
PROCESSOR_ARCHITECTURE x86
SystemDrive C:
APPDATA C:\Documents and Settings\kmyer\Application Data
windir C:\WINDOWS
USERPROFILE C:\Documents and Settings\kmyer

Or, if you you’d like information about a particular environment variable you could use a command similar to this:

$Env:os

Pretty easy, and pretty useful, stuff indeed.

TABLE 1

Note. You can also use the .NET Framework to return information about a particular environment variable:

[environment]::GetEnvironmentVariable("Sample","User")

In this case, we’re calling the GetEnvironmentVariable method, passing the method two parameters: Sample, the name of the environment variable we want to retrieve; and User, the type of environment variable. The User type is an environment variable tied to a user profile. You can also have Machine environment variables, which are tied to the computer as a whole, and Process environment variables, which are restricted to a single process.

That’s pretty cool. But what if you want to create a new environment variable, or maybe modify the value of an existing environment variable? What then?

Creating – and Modifying -- Environment Variables:

There are several different ways to create new environment variables using Windows PowerShell. For example, suppose all you need is a process-level environment variable (that is, an environment variable that is only visible to, and lasts only as long as, your current PowerShell session). In that case you can create the new environment variable using code similar to this:

$env:TestVariable = "This is a test environment variable."

As you can see, we didn’t have to do much here. We simply specified a name for our new environment variable (TestVariable) using the syntax $env:TestVariable. We then assigned this environment variable the value This is a test environment variable. If PowerShell can’t find an environment variable named TestVariable it will automatically create the variable and assign it the specified value. If it turns out that an environment variable named TestVariable already exists then PowerShell will simply assign the new value to the existing variable.

Which, as you might have guessed, is also one way you can change the value of an existing environment variable.

Like we said, though, that approach only works for process-level environment variables. To create more permanent environment variables (i.e., user-level or machine-level) you need to use the .NET Framework and the SetEnvironmentVariable method. For example, this command creates a user-level environment variable named TestVariable:

[Environment]::SetEnvironmentVariable("TestVariable", "Test value.", "User")

Here we’re using the .NET Framework’s System.Environment class and the SetEnvironmentVariable method. As you can see, we’re passing this method three parameters:

  • “TestVariable”, the name to be given to our new environment variable.

  • “Test value.”, the value to be assigned to the new environment variable.

  • “User” , which makes TestVariable a user-level environment variable. Alternatively, we could have set this to “Machine” (machine-level) or “Process” (process-level).

Again, if TestVariable does not exist PowerShell will create the new environment variable for us. If TestVariable does exist, then PowerShell will assign it the value Test value.

One thing to watch out for: when we used SetEnvironmentVariable to create a new user- or machine-level environment variable that variable didn’t always show up when we ran this command in Windows PowerShell:

Get-ChildItem Env:

Or at least it didn’t show up until we restarted PowerShell. (Or started up a new instance of PowerShell.) However, we could retrieve the value of the new variable at any time by using this command:

[Environment]::GetEnvironmentVariable("TestVariable","User")

Deleting Environment Variables

So how do you get rid of an environment variable after you no longer need it? Well, one way is to use the Remove-Item cmdlet, like so:

Remove-Item Env:\TestVariable

Alternatively you can use the SetEnvironmentVariable method, assigning the environment variable a null value:

[Environment]::SetEnvironmentVariable("TestVariable",$null,"User")

Again, if you use SetEnvironmentVariable the deleted variable might still show up when you call Get-ChildItem, at least until you restart. But take our word for it: the environment variable will no longer exist, as this command will demonstrate:

[Environment]::GetEnvironmentVariable("TestVariable","User")


Related Solutions

Windows PowerShell 1) Write a PowerShell Script to monitor a file for changes. 2) Write a...
Windows PowerShell 1) Write a PowerShell Script to monitor a file for changes. 2) Write a PowerShell Script to create a user account in a specific OU.
Give the PowerShell command that would display the “Path” environment variable. What two PowerShell commands will...
Give the PowerShell command that would display the “Path” environment variable. What two PowerShell commands will display all of the users currently logged into a server? What command would give helpful information about the Get-Command cmdlet.
Write a bash script named q1f.sh that will edit the PATH environment variable to include the...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the sourcefiles directory in your home directory and make the new variable global. Please show picture of output.
Write a PowerShell script which will prompt user to enter the number of the day of...
Write a PowerShell script which will prompt user to enter the number of the day of the week (e.g. 1,2,3,4,5,6,7) and return the day of the week. (e.g. Sunday...etc.) (Hint: Sunday is the 1st day of the week).
Write a PowerShell script that will ask your name, Date of Birth (DOB). Print a message...
Write a PowerShell script that will ask your name, Date of Birth (DOB). Print a message on console with a message like the examples. a. If the DOB you entered is in the future, print “Hello XXXXX, there are XXX days to your Birthday!” b. If the DOB you entered is today, print “Hello XXXXX, happy birthday!” c. If the DOB you entered is in the past, print “Hello XXXXX, your next birthday will be in XXX days.” d. Replace...
1. Write a simple batch include IFELSE statments? 2. Write a simple Powershell Script with IFELSE...
1. Write a simple batch include IFELSE statments? 2. Write a simple Powershell Script with IFELSE statements? 3. Explain this powershell script and write its output? $x = 30 if($x -eq 10){ write-host("Value of X is 10") } elseif($x -eq 20){ write-host("Value of X is 20") } elseif($x -eq 30){ write-host("Value of X is 30") } else { write-host("This is else statement") }
Using PowerShell ISE or VSCode, create a PowerShell script that will do the following: Create functions...
Using PowerShell ISE or VSCode, create a PowerShell script that will do the following: Create functions to get the following information Local username Local machine name Time zone Current Date Output the data that was collected to the screen Upload the script. Paste a screenshot of the results here
Write a script that will first initialize a string variable that will store x and y...
Write a script that will first initialize a string variable that will store x and y coordinates of a point in the form ‘x 3.1 y 6.4’. Then, use string manipulating functions to extract the coordinates and plot them. ANS % create a string variable of the form 'x 3.1 y 6.4' % extract the x and y coordinates and plot str = 'x 2.3 y 4.5'; [letter rest] = strtok(str); [x rest] = strtok(rest); [letter rest] = strtok(rest); y...
1. How to create a group of users using a PowerShell script. 2. how to create...
1. How to create a group of users using a PowerShell script. 2. how to create a file consisting a group of 3 new user name’s and passwords, 1 to a line in power shell. 3. How to write a script in power shell to delete specific group of users. Prove that it works.
write a matlab script for double mass spring system with animation.
write a matlab script for double mass spring system with animation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT