For some reason, one of our customers has a lot of problems connecting to our ftp, main reason being that he cant resolve our hostname (via IP no problem)
I've been looking around and found this code to add a line to the bottom of my host file (but its not working, just outputs the host file in its entirety)
Can someone explain how I can change this? It is fetching the IP perfectly, just need to either delete the non commented lines on host file or delete the last IP
$targethost="HOSTNAME"
$dnsserver="8.8.8.8"
$pattern = '^*' + $targethost + '.*$'
$file = "$($env:SystemRoot)\System32\drivers\etc\hosts"
$ip = Resolve-DnsName -Name $targethost -Type A -DnsOnly -Server $dnsserver
$hosts = Get-Content -Path $file
$hosts = $hosts | Foreach {
if ($_ -match $pattern)
{
$ip.IpAddress + " HOSTNAME "
}
else
{
# Keep current line
$_
}
}
#Uncomment this line to just view the output, no harm is done to the hosts file.
$hosts
# Uncomment this line to actually write the hosts file. Elevated privileges required.
#$hosts | Out-File $file -enc ASCII
Try
$hosts | Out-File -Encoding Ascii -append $file
or
$hosts | Add-Content $file #(ASCII by default)
A janky one, but it works
Clear-Content c:\windows\System32\drivers\etc\hosts
Set-Content -Path c:\windows\System32\drivers\etc\hosts -Value "Hello, World"
$targethost="HOSTNAME"
$dnsserver="8.8.8.8"
$pattern = '^*' + $targethost + '.*$'
$file = "$($env:SystemRoot)\System32\drivers\etc\hosts"
$ip = Resolve-DnsName -Name $targethost -Type A -DnsOnly -Server $dnsserver
$hosts = Get-Content -Path $file
$hosts = $hosts | Foreach {
if ($_ -match $pattern)
{
# Keep current line
$_
}
else
{
$ip.IpAddress + " HOSTNAME "
}
}
#Uncomment this line to just view the output, no harm is done to the hosts file.
$hosts
# Uncomment this line to actually write the hosts file. Elevated privileges required.
$hosts | Out-File $file -enc ASCII
Related
I'm writing a script to read some lines in a .txt and for each line I run a command. This command should generate an output with a file for each command run.
Currently it rewrites the with the result of the current command. Erasing what was written in the previous command.
can you help me?
$FILE = Get-Content "C:\Users\myuser\Desktop\adfs-groups.txt"
$FILENAME = Get-Random
foreach ($LINE in $FILE)
{
Write-Output "¨¨¨¨¨¨¨¨¨¨¨¨ Grupo: $LINE ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨"
Get-ADGroupMember -Identity $LINE | Select-Object SamAccountName,name
Out-File C:\Users\myuser\Desktop\test_random\$FILENAME.txt
}
Problem: I need to search for some text inside a file, it contains three consecutive lines of text. How do I verify (find the existence of) those lines are in the file?
Expected Return Value: A boolean
Example Input File: text.txt
one
two
three
four
five
Example Pattern to Search For
two
three
four
Simple Answer:
$file = (Get-Content -Raw file.txt) -replace "`r" # removing "`r" if present
$pattern = 'two
three
four' -replace "`r"
$file | Select-String $pattern -Quiet -SimpleMatch
RE-EDIT. Wow. This is a tricky way to do it. At the prompt, $pattern has no "`r", but in a script it does. This should work as a script or at the prompt.
$file = (get-content -raw file.txt) -replace "`r"
$pattern = 'two
three
four' -replace "`r"
# just showing what they really are
$file -replace "`r",'\r' -replace "`n",'\n'
$pattern -replace "`r",'\r' -replace "`n",'\n'
# 4 ways to do it
$file -match $pattern
$file | select-string $pattern -quiet -simplematch
$file -like "*$pattern*"
$file.contains($pattern)
# output
one\ntwo\nthree\nfour\nfive\n
two\nthree\nfour
True
True
True
True
Hmm, trying a regex way. In single line mode, a . can match a "`r" or a "`n".
$file = get-content -raw file.txt
$pattern = '(?s)two.{1,2}three.{1,2}four'
# $pattern = 'two\r?\nthree\r?\nfour'
# $pattern = 'two\r\nthree\r\nfour'
# $pattern = 'two\nthree\nfour'
$file -match $pattern
$file | select-string $pattern -quiet
I have to write the contents of two directories to a CSV where Directory 1 is Column 1 and Directory 2 is Column 2.
I am fairly new to Powershell so please be gentle.
Here is what I have tried:
$source_loc = "Z:\Dir1"
$dest_loc = "Y:\Dir2"
$outfile = "C:\Outfile.csv"
$source = get-ChildItem $source_loc
$dest = get-ChildItem $dest_loc
Remove-Item $outfile -ErrorAction Ignore
Add-Content $outfile "Old URL, New URL"
$max = ( $source | Measure-Object ).Count
For ( $i = 0; $i -lt $max; $i++ )
{
$cur = $source | Select-Object -Index $i | Select Name
$old = "http://thisisawebsite.web/oldurl/" + $cur
$cur = $dest | Select-Object -Index $i | Select Name
$new = "http://thisisawebsite.web/newurl/" + $cur
$line = $old + ',' + $new
Add-Content $outfile $line
}
The problem I am having is that right now the output of Outfile.csv looks like this:
http://thisisawebsite.web/oldurl/#{Name=File1.ext},http://thisisawebsite.web/newurl/#{Name=File1.ext}
http://thisisawebsite.web/oldurl/#{Name=File2.ext},http://thisisawebsite.web/newurl/#{Name=File2.ext}
When I want it to look like this:
http://thisisawebsite.web/oldurl/File1.ext,http://thisisawebsite.web/newurl/File1.ext
http://thisisawebsite.web/oldurl/File2.ext,http://thisisawebsite.web/newurl/File2.ext
I tried converting to arrays and indexing into it but it seems to do the same thing. Do I need to do a substr on the $cur variable and ignore the first 7 characters and then ignore the last one? I know my code is rubbish so please try your best not to insult me.
Thanks! ^.^
There are a few simpler ways of doing what you're trying to do, I believe. A quick and dirty way of making this work, however, is expanding the "name" property that you're selecting. Try this small snippet on the two noted lines :
$cur = $source | Select-Object -Index $i | Select -ExpandProperty Name
$cur = $dest | Select-Object -Index $i | Select -ExpandProperty Name
This should select the Value for the "name" property instead of a hashtable including both.
Another useful thing to note for the future is that with double quotes, you don't need to manually concatenate a variable and a string in powershell.
$new = "http://thisisawebsite.web/newurl/" + $cur
Is the same as
$new = "http://thisisawebsite.web/newurl/$cur"
Actually beginner in PowerShell. I want to convert shell script into powershell.
I have sample.txt file which contains 3 lines. I want to select each word in line to different variable and which variables are need to use in next if condition.
get-content sample.txt
wordone|secondword|thridword|fourthword
wordone1|secondword1|thridword1|fourthword1
wordone2|secondword2|thridword2|fourthword2
I have already shell script:
grep "^[^#]" ./sample.txt > test.txt
for line in `cat test.txt`
do
dir=`echo $line | cut -d'|' -f1`
metadata_name=`echo $line | cut -d'|' -f2`
metadata_file=`echo $line | cut -d'|' -f3`
config_file=`echo $line | cut -d'|' -f4`
if [ "${config_file}" != "" ]
then
cp ${dir}.xml folder1/.
Please help me how to convert shell to powershell.
Is this what you are looking for?
$textfile = get-content "sample.txt"
$sample = "sample.txt"
for ($i=0;$i -lt $textfile.count;$i++){
$textrow = ($textfile[$i]).split("|")
$dir = $textrow[0]
$metadata_name = $textrow[1]
$metadata_file = $textrow[2]
$config_file = $textrow[3]
If($config_file -ne ""){
New-Item $dir/ConfigurationPlan -type directory -force
$sample | Out-File "$dir/ConfigurationPlan/$($config_file)_sample.xml"
}
}
I'd treat your .\sample.txt file as a csv file and use Import-Csv with -Delimiter '|' and -Header dir,metadata_name,metadata_file,config_file parameters.
Then iterate the lines checking for dir and existence of the file to copy.
$Sample = Import-Csv sample.txt -delimiter '|' -Header dir,metadata_name,metadata_file,config_file
$Sample
"----"
ForEach ($Line in $Sample){
if ($Line.config_file -ne ""){
$File = "$($line.dir).xml"
"config_file is $($Line.config_file) checking $File"
if (Test-Path $File){
Copy-ITem -Path $File -Destination "folder1\" -whatif
}
}
}
Sample output:
> Q:\Test\2017\07\21\SO_45239319.ps1
dir metadata_name metadata_file config_file
--- ------------- ------------- -----------
wordone secondword thridword fourthword
wordone1 secondword1 thridword1 fourthword1
wordone2 secondword2 thridword2 fourthword2
----
config_file is fourthword checking wordone.xml
config_file is fourthword1 checking wordone1.xml
config_file is fourthword2 checking wordone2.xml
If your output looks OK, remove the -WhatIf parameter of Copy-Item
How can I batch append a directory of files using the window cmd so that they change from.
"file1"
"file2"
"file3"
to
"001 file1"
"002 file2"
"003 file3"
If you're willing to PowerShell instead of batch, you could do this as a one-off:
gci |% { ren $_ (('{0:d3} ' -f $i++) + $_.Name) }
Which is more fully written as:
$counter = 0
Get-ChildItem | ForEach-Object {
$prefix = '{0:d3}' -f $counter++
Rename-Item -Path $_.FullName -NewName "$prefix $($_.Name)"
}