英文:
Creating a symbolic link using PowerShell
问题
I have provided the translated code portions below:
我在使用PowerShell方面遇到了一些困难,我觉得应该可以很容易地完成,希望能得到一些帮助。
我有一系列存储在`C:\temp`内的日志和配置文件路径。不仅如此,我需要在每次用户运行应用程序(通过批处理文件启动)时删除临时文件,然后创建一个符号链接到登录用户的OneDrive中。
因此,我需要运行大约10个文件夹,如下所示:
```none
rmdir /Q /S c:\temp\app\cgi-bin
rmdir /Q /S c:\temp\app\contrib
rmdir /Q /S c:\temp\app\logs
等等。
...然后运行以下相应的命令:
mklink /D c:\temp\app\cgi-bin c:\users\<logged on user>\One Drive - Company Name\app\cgi-bin
mklink /D c:\temp\app\contrib c:\users\<logged on user>\One Drive - Company Name\app\contrib
mklink /D c:\temp\app\logs c:\users\<logged on user>\One Drive - Company Name\app\logs
我尝试在PowerShell中完成这个任务,如下所示:
$username = $env:username
(get-item C:\temp\app\cgi-bin).Delete()
New-item -ItemType SymbolicLink -path c:\temp\app\cgi-bin -target 'C:\users$username\OneDrive - Company Name\app\cgi-bin\'
...然后将它运行到每个位置:
$username = $env:username
(get-item C:\temp\app\contrib).Delete()
New-item -ItemType SymbolicLink -path c:\temp\app\contrib -target 'C:\users$username\OneDrive - Company Name\app\contrib\'
等等。
但是我感到非常困惑。我觉得应该有一种简单的方法来循环遍历原始文件夹位置,然后将它们连接在一起。我尝试使用join-path
,但也没有成功:
New-item -ItemType SymbolicLink -path C:\temp\app\cgi-bin -target (join-path 'C:\users' '"OneDrive - Company Name\app\cgi-bin\"')
请注意,我已将原文中的HTML实体编码符号(如`<`和`>`)保留在翻译中,以保持文本的一致性。这些符号通常用于表示尖括号(`<`和`>`)。如果需要进一步的协助,请随时提出。
<details>
<summary>英文:</summary>
I am struggling with a bit of PowerShell, which I feel I should be able to do easily, and would appreciate some help.
I have a series of paths storing log and config files within `C:\temp`. Not only that, but I need to delete the temp file and then create a symbolic link to the logged on user's OneDrive every time they run the app (which is launched with a batch file).
So I need it to run through about 10 folders such as
```none
rmdir /Q /S c:\temp\app\cgi-bin
rmdir /Q /S c:\temp\app\contrib
rmdir /Q /S c:\temp\app\logs
etc.
...and then run the equivalent of:
mklink /D c:\temp\app\cgi-bin c:\users\<logged on user>\One Drive - Company Name\app\cgi-bin
mklink /D c:\temp\app\contrib c:\users\<logged on user>\One Drive - Company Name\app\contrib
mklink /D c:\temp\app\logs c:\users\<logged on user>\One Drive - Company Name\app\logs
What I have attempted in PS to do this is below:
$username = $env:username
(get-item C:\temp\app\cgi-bin).Delete()
New-item -ItemType SymbolicLink -path c:\temp\app\cgi-bin -target 'C:\users$username\OneDrive - Company Name\app\cgi-bin\'
...and then was going to get it to run the same for each location:
$username = $env:username
(get-item C:\temp\app\contrib).Delete()
New-item -ItemType SymbolicLink -path c:\temp\app\contrib -target 'C:\users$username\OneDrive - Company Name\app\contrib\'
etc.
...but have got completely lost. I feel there should be an easy way to loop through the original folder locations and then joining the parts together. I had another go with join-path
, but it didn't go too well either:
New-item -ItemType SymbolicLink -path C:\temp\app\cgi-bin -target (join-path 'C:\users' '"OneDrive - Company Name\app\cgi-bin\"')
答案1
得分: 0
以下是您要翻译的部分:
"Just, first, as you're new here -- so Welcome ! --, it doesn't really matter, but keep in mind to post the errors you are getting, because I had to figure them out here 😃.
Right, just a few things first :
- to loop through an array, you can use the
foreach(){}
method. - as @Compo stated, you can use
$env:onedrive
to get the current user's OneDrive directory ($env
is the environment variable, it's a preset system-wide variable basically containing user-useful settings) - you cannot create a symbolic link pointing to a non-existing directory, so here, if the folders in OneDrive don't exist, it won't work; so I'm adding a line here to check if the folder exists, and create it if not, just make sure
app
exists !
$onedrive = "$env:onedrive - Company Name"
foreach ($folder in (Get-ChildItem -Path "C:\temp\app").Name){
Remove-Item -Path $folder
$onedrivePath = "$onedrive\app$folder"
if (-not(Test-Path $onedrivePath)){
New-Item -ItemType Directory -Path $onedrivePath
}
New-item -ItemType SymbolicLink -Path "c:\temp\app$folder" -target $onedrivePath
}
Feel free to comment if you want more explanations, and have a wonderful PowerShell journey (PowerShell is one of those incredible but underestimated languages...)
Silloky
EDIT :
After modifications to the script as you requested, here it is in all its beauty !
$onedrive = "$env:onedrive"
$pathArray = @(
"contrib",
"htdocs",
"install",
"licenses",
"locale",
"apache\logs",
"mysql\data",
"mysql\backup",
"perl",
"php",
"phpMyAdmin",
"sendmail",
"tmp",
"tomcat",
"webdav"
)
foreach ($path in $pathArray) {
$localpath = "c:\app$path"
if (Test-Path $localpath) {
Remove-Item -path $localpath -Recurse -force -Confirm:$false
}
# Write-Output "Setting OneDrive Path"
$onedrivePath = "$onedrive\app$path"
if (-not(Test-Path $onedrivePath)) {
New-Item -ItemType Directory -Path $onedrivePath
}
# Write-Output "Creating Junction"
New-item -ItemType Junction -Path "$localpath" -target $onedrivePath
}
I replaced Read-Host
by Write-Output
as it is more adequate : Read-Host
prompts for user input (like input()
in Python) when Write-Output
prints to the console (like print()
in Python), which is what you want assuming from the "prompt" texts.
I've also organised the array in much more semantic way, making it easier to edit.
Silloky
英文:
Just, first, as you're new here -- so Welcome ! --, it doesn't really matter, but keep in mind to post the errors you are getting, because I had to figure them out here 😅.
Right, just a few things first :
- to loop through an array, you can use the
foreach(){}
method. - as @Compo stated, you can use
$env:onedrive
to get the current user's OneDrive directory ($env
is the environment variable, it's a preset system-wide variable basically containing user-useful settings) - you cannot create a symbolic link pointing to a non-existing directory, so here, if the folders in OneDrive don't exist, it won't work; so I'm adding a line here to check if the folder exists, and create it if not, just make sure
app
exists !
$onedrive = "$env:onedrive - Company Name"
foreach ($folder in (Get-ChildItem -Path "C:\temp\app").Name){
Remove-Item -Path $folder
$onedrivePath = "$onedrive\app$folder"
if (-not(Test-Path $onedrivePath)){
New-Item -ItemType Directory -Path $onedrivePath
}
New-item -ItemType SymbolicLink -Path "c:\temp\app$folder" -target $onedrivePath
}
Feel free to comment if you want more explanations, and have a wonderful PowerShell journey (PowerShell is one of those incredible but underestimated languages...)
Silloky
EDIT :
After modifications to the script as you requested, here it is in all its beauty !
$onedrive = "$env:onedrive"
$pathArray = @(
"contrib",
"htdocs",
"install",
"licenses",
"locale",
"apache\logs",
"mysql\data",
"mysql\backup",
"perl",
"php",
"phpMyAdmin",
"sendmail",
"tmp",
"tomcat",
"webdav"
)
foreach ($path in $pathArray) {
$localpath = "c:\app$path"
if (Test-Path $localpath) {
Remove-Item -path $localpath -Recurse -force -Confirm:$false
}
# Write-Output "Setting OneDrive Path"
$onedrivePath = "$onedrive\app$path"
if (-not(Test-Path $onedrivePath)) {
New-Item -ItemType Directory -Path $onedrivePath
}
# Write-Output "Creating Junction"
New-item -ItemType Junction -Path "$localpath" -target $onedrivePath
}
I replaced Read-Host
by Write-Output
as it is more adequate : Read-Host
prompts for user input (like input()
in Python) when Write-Output
prints to the console (like print()
in Python), which is what you want assuming from the "prompt" texts.
I've also organised the array in much more semantic way, making it easier to edit.
Silloky
答案2
得分: 0
$onedrive = "$env:onedrive"
$pathvariable = @("contrib", "htdocs", "install", "licenses", "locale", "apache\logs", "mysql\data", "mysql\backup", "perl", "php", "phpMyAdmin", "sendmail", "tmp", "tomcat", "webdav")
foreach ($var in $pathvariable) {
$localpath = "c:\app$var"
if (Test-Path $localpath){
Remove-Item -path $localpath -Recurse -force -Confirm:$false
}
##Read-Host -Prompt "Setting OneDrive Path"
$onedrivePath = "$onedrive\app$var"
if (-not(Test-Path $onedrivePath)){
New-Item -ItemType Directory -Path $onedrivePath
}
##Read-Host -Prompt "Creating Junction"
New-item -ItemType Junction -Path "$localpath" -target $onedrivePath
}
英文:
$onedrive = "$env:onedrive"
$pathvariable = @("contrib", "htdocs", "install", "licenses", "locale", "apache\logs", "mysql\data", "mysql\backup", "perl", "php", "phpMyAdmin", "sendmail", "tmp", "tomcat", "webdav")
foreach ($var in $pathvariable) {
$localpath = "c:\app$var"
if (Test-Path $localpath){
Remove-Item -path $localpath -Recurse -force -Confirm:$false
}
##Read-Host -Prompt "Setting OneDrive Path"
$onedrivePath = "$onedrive\app$var"
if (-not(Test-Path $onedrivePath)){
New-Item -ItemType Directory -Path $onedrivePath
}
##Read-Host -Prompt "Creating Junction"
New-item -ItemType Junction -Path "$localpath" -target $onedrivePath
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论