Powershell - Trying to test-path against a string that resembles a variable name - variables

The company I work for prefixes all their admin level accounts with a $ sign. Trouble, I know.
I'm trying to use test-path to see if a folder exists like so:
$username = read-host "Enter Login ID:"
I type $adminsdb into the box and hit ok
##### Find TS Profile #####
$TSProfile_exist = test-path "\\server\tsprofiles$\$username"
The folder DOES exist but... $TSProfile_exist is coming up False
How do I handle the $ in the username? I'm building this app to bring up quick stats on users in the environment. We also have service accounts that are prefixed with # signs.

The way to handle special caracters in PowerShell is using ` (backtick)
$TSProfile_exist = test-path "\\server\tsprofiles$\$username"
becomes
$TSProfile_exist = test-path "\\server\tsprofiles`$\$username"
Be careful a trick with Test-Path, is that it semantic is not that the directory exists, but is that the directory is readable. In other words if you do not have access to the directory you test, you will receive false even if the directory exists. See this other entry.

The other alternative is to use single-quoted strings; powershell will not expand variables in this case.
test-path '\\server\path\$username'
-Oisin

Related

Run sql plus commands on Unix machine

I am working on a unix machine and the only way to execute oracle sql commands is through a unix script we grant access like this :
#! /user/bin/ksh
User = 'PATH' # I can't read the file in this path
sqlplus $user << word # I don't know what it is used for
And then I start writing sql commands then execute the script through cmd
My question is:
Do I have any way to login to sqlplus directly through the info above through cmd
I tried to use this command to log in directly to SQL*Plus:
sqlplus $user << word # I don't know what it is used for
But it prompted username: # which I don't know
User = 'PATH' # I can't read the file in this path
There is no file to read. You are assigning the string literal 'PATH' to the environment variable "User". You could just as well say "User = 'FUBAR'"
sqlplus $user << word # I don't know what it is used for
It is telling the OS to launch the executable 'sqlplus', pass it the value of the environment variable "$user", then redirect other input from the next lines of the script until it comes to the string literal 'word'. This is call 'input redirection, and the commands between this line and the line beginning with the word 'word' are sometimes referred to as "a 'here' document". Using the string literal 'word' to terminate it is wierd and misleading at best. Most people use some variation of 'EOF' for this purpose.
I don't know what it is expected to be used for either. In *nix, environment variables (as are file names) are case sensitive. So this variable , "user" is not the same variable, "User" as you set in the previous line.
And then i start writing sql commands then execute the script through
cmd
I'm not sure what you mean by this. Are you saying that at this point, your script has sql commands intended to be processed by sqlplus? As indicated by your use of input redirection?
But it prompted username : # which I don't know
Well, in spite of all the other issues, if you don't know the username and password, you will never be able to connect to the database.
If your unix box is setup correctly the variable PATH should include /usr/local/bin you can test by typing in the command echo $PATH.
if its setup, the source in the oracle file oraenv like so
. oraenv
Note there should be a space between the period a the word oraenv. By doing this it should append the directories $ORACLE_HOME:$ORACLE_HOME/bin to the PATH variable. Since sqlplus is in $ORACLE_HOME/bin it should now be found.
I wouldn't recommend deviating from this standard. You should speak to your unix admin and Oracle dba to make sure this is setup correctly

BAT or Powershell For loop through CSV to build a URL

Solved. So my first go at this post was a VERY poorly structured question trying to obfuscate proprietary company information in a very poor manner, and not asking the question well.
Once Walter even got me thinking in the correct direction i worked through the issue. Below was the second issue i was running into and found that the #{key=value} statement was being passed into my url because for some reason my script did not like the header in my csv file. In hindsight, perhaps because i was naming my variable the same as my header. Regardless I worked around it just by using Get-Content rather than Import-CSV.
$aliases = Import-Csv -Path .\aliases.csv
foreach ($alias in $aliases) {
Write-Output ('http://www.' + $($alias) + '.mydomain.com') >> urls.txt
where the contents of aliases.csv is:
alias
Matthew
Mable
Mark
Mary
This is giving me:
http://www.#{alias=Matthew}.mydomain.com
http://www.#{alias=Mable}.mydomain.com
http://www.#{alias=Mark}.mydomain.com
http://www.#{alias=Mary}.mydomain.com
When successful urls.txt should contain:
http://www.Matthew.mydomain.com
http://www.Mable.mydomain.com
http://www.Mark.mydomain.com
http://www.Mary.mydomain.com
NOTE: Edited to clarify use case
In Powershell
Get-Content names.txt | %{"Hello, my name is $_. How are you?"} >> results.txt
By the way, with just a little more effort, you can read more than one variable from a csv file, and substitute all of them for named variables in the text. This turns out to be very useful in a variety of situations.
Edit to conform to your edit
Import-csv ./aliases.csv | %{ "http://www.$($_.alias).mydomain.com"}
Notes:
Once you get used to them, pipelines are the easiest way to process a stream of just about anything.
% is an abbreviation of Foreach-Object (not to be confused with foreach).
The loop will be done once for each object coming out of the pipe. Each object will be a PSCustomObject with a single property named alias.
$() allows evaluation of a subexpression within a double quoted string.
$_ is the current object.
the dot, in this context, separates an object specified from a named property.

How can I use source script with variables in CygWin?

I'm trying to use external script with variables, but in result I get only "no such file or directory".
1st.ksh
#!bin/ksh
PATHNAME = `dirname $0`
. $PATHNAME/2nd.ksh
Echo $EXTVAR
2nd.ksh
#!bin/ksh
EXTVAR=1
I tried to use "Source" instead of "." (Source $PATHNAME/2nd.ksh) and I get the same result.
To run script I'm using full path to the script - cygdrive/e/Folder/1st.ksh.
2nd.ksh in this path too (cygdrive/e/Folder/).
All rights was granted for both files (chmod u=rwx,g=rwx,o=rwx filename).
If I put files in cygwin home path (/home/username/) I have the same.
Please help to understand what I'm doing wrong.
Thanks in advance!
$() should be used in ksh instead of `` (link)
. should be user instead of source (link)
"=" must not be surrounded with spaces. You should write: PATHNAME=$(dirname $0)
you should be aware of case-sensitiveness: echo, source

Find all users, use them as variable to pull appdata directory list from cmd?

I'm trying to build out a script that will pull the list of users, grab each of their names and then do a directory list for each of their appdata folders.
So far I'm thinking that I could just use the net user > C:\userlist.txt to create the list of users, my real problem comes in grabbing the user's names and putting them into the next command.
By using a variable of some kind I would like to have findstr pull in each user so that I could get a list of all the users's directories.
The end result would look something like this:
dir C:\users\$variableforusers\AppData\Roaming > C:\directorylist.txt
I'd like to be able to get this command to run for each users appdata directory, so this command would need to repeat itself based on how many users the end machine has.
This will be running from the local system account so %appdata% will not work as it only gives you the currently logged in user's appdata folder.
If possible, I'd like for the directory list to all be dumped into one file, otherwise I'd have to introduce a variable to check multiple files for the results.
Thanks ahead of time for the help guys!
I think you should not use user's name to obtain a path to user's profile directory. The directory name can be different than user's name - for example in case user name hsa been changed after the profile was created or if there is a user name conflict (a local user has the same username as a domain user).
You can do a dir of AppData\Roaming for each subdirectory of C:\Users using the following syntax:
for /d %A in (C:\Users\*) do if exist %A\AppData\Roaming dir %A\AppData\Roaming
But again this solution will omit any user profiles that are stored outside C:\Users.
To do a dir of AppData\Roaming directory of each user profile, you can get the paths of user profiles from registry:
for /f "tokens=3" %A in ('reg query "HKLM\Software\Microsoft\WindowsN T\CurrentVersion\ProfileList" /s /v ProfileImagePath ^| find "REG_EXPAND_SZ"') do #dir %A\AppData\Roaming
If you want to use these commands in a script, use double percent signs (%%A).
To learn more about for command type for /?

batch scripting: how to get parent dir name without full path?

I'm working on a script that processes a folder and there is always one file in it I need to rename. The new name should be the parent directory name. How do I get this in a batch file? The full path to the dir is known.
It is not very clear how the script is supposed to become acquainted with the path in question, but the following example should at least give you an idea of how to proceed:
FOR %%D IN ("%CD%") DO SET "DirName=%%~nxD"
ECHO %DirName%
This script gets the path from the CD variable and extracts the name only from it to DirName.
You can use basename command:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=$(basename "$FULLPATH")
You can use built-in bash tricks:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=${FULLPATH##*/}
Explanations:
first # means 'remove the pattern from the begining'
second # means 'remove the longer possible pattern'
*/ is the pattern
Using built-in bash avoid to call an external command (i.e. basename) therefore this optimises you script. However the script is less portable.